0

I have a phrase on a string and I want to split it on other 5 or more string without spaces. For example:

String test = "hi/ please hepl meok?";

and I want :

String temp1 = "hi/";
String temp2 = "please";
String temp3 = "help";
String temp4 = "meok?";

I dont want to add that in an array, because I want to split the temp4 to 3 more strings. eg

->> temp4 after splitting:

temp4 = "me"
temp5 = "ok"
temp6 = "?"

This Question is asked because I want to write a method to decode a String phrase from a LinkedHashMap set with some decodes. Thanks. If my way is wrong please guide me! :)

us2012
  • 16,083
  • 3
  • 46
  • 62
John
  • 31
  • 1
  • 3
  • 8
  • Check out the [`String.split()`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)) method from the standard API. With the right parameter value, you can do all splits with one call rather than needing to manually split individual Strings further. – Code-Apprentice Jan 09 '13 at 01:45
  • In your example of spliting `temp4`, how do you know where to split it? – Code-Apprentice Jan 09 '13 at 01:47
  • @Code-Guru it tried, my code for this is: ` String temp = codedText; temp.split(codedText); System.out.println(temp.split(codedText));` I will use substring method that it will check the Set – John Jan 09 '13 at 01:49
  • What happened when you did that? (You might want to read the doc that I linked to. For one thing, you are not sending the correct parameter value for what you described in your original post.) – Code-Apprentice Jan 09 '13 at 01:51
  • on my console from println I have this :java.lang.String;@2554aed9 only his memory address. @Code-Guru – John Jan 09 '13 at 01:57
  • According to the Java docs that I linked earlier, `String.split()` returns an array. When you send an array to `println()`, you get output similar to what you see. Try printing out each individual element of the array instead. (The `toString()` methods from [`java.util.Array`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html) might be helpful.) – Code-Apprentice Jan 09 '13 at 02:01

2 Answers2

5

I dont want to add that in an array, because I want to split the temp4 to 3 more strings. eg

Split the string with String#split, then assign the parts of the resulting array to your individual variables:

String[] parts = theOriginalString.split(" ");
String temp1 = parts[0];
String temp2 = parts[1];
String temp3 = parts[2];
String temp4 = parts[3];
String temp5 = parts[4];

I find the idea of making these separate named variables a bit suspect, but I can see use cases — for instance, if you're about to embark on a bunch of logic where useful names make the code clearer.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • The reason that I dont want an array is that this phase can be 10char long or 20. I so I dont know the maximum number of array, so I dont where to stop. I am thinking arraylist as a different way of string array b/c it has arraylist.size() so It will be the max. @T.J. Crowder – John Jan 09 '13 at 02:02
  • 1
    @John You don't need to know the size of the array. `String.split()` creates an array of the correct size for you. – Code-Apprentice Jan 09 '13 at 02:06
  • 1
    @John: *"The reason that I dont want an array is that this phase can be 10char long or 20. I so I dont know the maximum number of array, so I dont where to stop."* Then surely it's impossible for you to declare the variables and you *have* to use an array, or `List`, or similar. If your concern is that you'll get an overly-massive array, you can use the [limited version of `String#split`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String,%20int)) instead. – T.J. Crowder Jan 09 '13 at 02:06
  • @T.J. Crowder Ok, but I need to know something. Is String[] has a method that returns the max number of this array? eg String[] parts = theOriginalString.split(" "); anything like parts.size() ? parts.maxCels() ? – John Jan 09 '13 at 02:18
  • @John: [Arrays have a `length` property](http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.3) telling you how long they are. If an array's `length` is `3`, it has elements `0`, `1`, and `2`. – T.J. Crowder Jan 09 '13 at 02:25
  • ok!! thanks! :) I ll try this one and probably my problem will be solved! – John Jan 09 '13 at 02:36
1

Given your string, split() will do the trick

String tokens[] = test.split(" ");

tokens[0] will then be "hi/", tokens[1] will be "please" and so on.

EDIT you're going to be storing your strings in an array first in any case when split is used, use StringTokenizer if you want to loop through them individually.

 StringTokenizer st = new StringTokenizer(test);
 while (st.hasMoreTokens()) {
     System.out.println(st.nextToken());
 }
ryanbwork
  • 2,123
  • 12
  • 12
  • 1
    Question explicitly says 'no array'. Whether that makes sense or not, who knows, but still - this doesn't answer the question! – us2012 Jan 09 '13 at 01:48
  • @us2012 If a "I can't use XXX" requirement is because of a homework restriction, then I would agree because these kinds of restrictions are meant to help learn features of a language. In this, the requirement to avoid arrays doesn't seem to be one of this type, though. – Code-Apprentice Jan 09 '13 at 02:08