2

I have a lengthy string in my Android program. What I need is, I need to split each word of that string and copy that each word to a new String Array. For eg: If the string is "I did android program" and the string array is named my_array then each index should contain values like:

my_array[0] = I
my_array[1] = did
my_array[2] = Android
my_array[3] = Program

A part of program which I did looks like this:

StringTokenizer st = new StringTokenizer(result,"|");
Toast.makeText(appointment.this, st.nextToken(), Toast.LENGTH_SHORT).show();
while(st.hasMoreTokens())
{
String n = (String)st.nextToken();
services1[i] = n;
Toast.makeText(appointment.this, st.nextToken(), Toast.LENGTH_SHORT).show();
}

Can any one please suggest some ideas..

Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115
Sanjeev
  • 267
  • 2
  • 4
  • 8

4 Answers4

9

Why not use String.split() ?

You can simply do

String[] my_array = myStr.split("\\s+");
Gopi
  • 10,073
  • 4
  • 31
  • 45
1

Since '|' is a special character in regular expression, we need to escape it.

for(String token : result.split("\\|"))
        {
            Toast.makeText(appointment.this, token, Toast.LENGTH_SHORT).show();
        }
st0le
  • 33,375
  • 8
  • 89
  • 89
0

Since String is a final class, it is by default immutable, which means you cannot make changes to your strings. If you try, a new object will be created, not the same object modified. Therefore if you know in advance that you are going to need to manipulate a String, it is wise to start with a StringBuilder class. There is also StringBuffer for handling threads. Within StringBuilder there are methods like substring():

substring(int start)
Returns a new String that contains a subsequence of characters currently contained in this character sequence.

or getChars():

getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Characters are copied from this sequence into the destination character array dst.

or delete():

delete(int start, int end)
Removes the characters in a substring of this sequence.

Then if you really need it to be a String in the end, use the String constructor(s)

String(StringBuilder builder)
Allocates a new string that contains the sequence of characters currently contained in the string builder argument.

or

String(StringBuffer buffer)
Allocates a new string that contains the sequence of characters currently contained in the string buffer argument.

Although to understand when to use String methods and when to use StringBuilder, this link or this might help. (StringBuilder comes in handy with saving on memory).

Community
  • 1
  • 1
Azurespot
  • 3,066
  • 3
  • 45
  • 73
0

You can use String.split or Android's TextUtils.split if you need to return [] when the string to split is empty.

From the StringTokenizer API docs:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

JRL
  • 76,767
  • 18
  • 98
  • 146