252

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() of 2 different TextViews to display that string.

What would be the best way to approach this?

2
  • You could probably try reading into regular expressions. They work fine too. Commented Sep 17, 2010 at 5:18
  • 10
    @Falmarri - Any unique question about programming is welcome on Stack Overflow. Commented Feb 4, 2011 at 14:30

6 Answers 6

672
String currentString = "Fruit: they taste good";
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();

If you want to split the string with a special character like dot(.) you should use escape character \ before the dot

Example:

String currentString = "Fruit: they taste good.very nice actually";
String[] separated = currentString.split("\\.");
separated[0]; // this will contain "Fruit: they taste good"
separated[1]; // this will contain "very nice actually"

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
Sign up to request clarification or add additional context in comments.

13 Comments

Thanks for this! Also useful for separating hour and minute when creating a new Time object.
Thank You! .split() method doesn't works at all in Android! StringTokenizeris working fine.
Yes it does... what problems did you have?
split in android receives a regular expression instead of a simple string divider.
@HardikParmar use etPhoneNo.getText().toString().replaceAll("\\D", ""); its says that replace all which is not digit
|
88

.split method will work, but it uses regular expressions. In this example it would be (to steal from Cristian):

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

Also, this came from: Android split not working correctly

Comments

56

android split string by comma

String data = "1,Diego Maradona,Footballer,Argentina";
String[] items = data.split(",");
for (String item : items)
{
    System.out.println("item = " + item);
}

Comments

26
     String s = "having Community Portal|Help Desk|Local Embassy|Reference Desk|Site News";
     StringTokenizer st = new StringTokenizer(s, "|");
        String community = st.nextToken();
        String helpDesk = st.nextToken(); 
        String localEmbassy = st.nextToken();
        String referenceDesk = st.nextToken();
        String siteNews = st.nextToken();

Comments

22

You might also want to consider the Android specific TextUtils.split() method.

The difference between TextUtils.split() and String.split() is documented with TextUtils.split():

String.split() returns [''] when the string to be split is empty. This returns []. This does not remove any empty strings from the result.

I find this a more natural behavior. In essence TextUtils.split() is just a thin wrapper for String.split(), dealing specifically with the empty-string case. The code for the method is actually quite simple.

3 Comments

What's the benefit of using TextUtils.split() instead of just calling split() directly on the string?
Edited answer to clarify difference between TextUtils.split() and String.split()
Thanks, I actually read the documentation for TextUtils.split() but for some reason I missed this detail. I guess I was to tired to understand what it actually said.
0

String s = "String="

String[] str = s.split("="); //now str[0] is "hello" and str[1] is "goodmorning,2,1"

add this string

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.