Android Split string

JavaAndroidString

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

.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: https://stackoverflow.com/questions/6965642/android-split-not-working-correctly

Solution 3 - Java

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);
}

Solution 4 - Java

     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();

Solution 5 - Java

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. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.L_preview/android/text/TextUtils.java#TextUtils.split%28java.lang.String%2Cjava.util.regex.Pattern%29">The code for the method is actually quite simple.

Solution 6 - Java

String s = "String="

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

add this string

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzaidView Question on Stackoverflow
Solution 1 - JavaCristianView Answer on Stackoverflow
Solution 2 - JavaSilas GreenbackView Answer on Stackoverflow
Solution 3 - JavamahasamView Answer on Stackoverflow
Solution 4 - JavaFaakhirView Answer on Stackoverflow
Solution 5 - JavagardarhView Answer on Stackoverflow
Solution 6 - Javauser11716837View Answer on Stackoverflow