views:

164

answers:

1

i have a string called CurrentString and is in the form of something like this "Fruit: they taste good". i would like to split up the CurrentString using the : as the delimiter.so that way the word "Fruit" will be split into its own string and "they taste good" will be another string.and then i would simply like to use SetText and 2 different textviews to display that string.

what would be the best way to approach this?

+2  A: 

@Alex, that's a good answer, and answers go here.

@zaid This is what you can do:

String[] separated = CurrentString.split(":");
separated[0]; // this will contain "Fruit"
separated[1]; // this will contain " they taste good"

You may want to remove the space to the second String:

separated[1] = separated[1].trim();

There are other ways to do it. For instance, you can use the StringTokenizer class (from java.util):

StringTokenizer tokens = new StringTokenizer(CurrentString, ":");
String first = tokens.nextToken();// this will contain "Fruit"
String second = tokens.nextToken();// this will contain " they taste good"
// in the case above I assumed the string has always that syntax (foo: bar)
// but you may want to check if there are tokens or not using the hasMoreTokens method
Cristian
Thank you for the terrific reply. i used the StringTokenizer and it worked perfectly.
zaid