249

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?

Bhavin Nattar
  • 3,189
  • 2
  • 22
  • 30
zaid
  • 6,259
  • 7
  • 26
  • 20

6 Answers6

669
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
devu mani
  • 337
  • 6
  • 12
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • Thanks for this! Also useful for separating hour and minute when creating a new Time object. – worked Sep 28 '11 at 12:24
  • 24
    Thank You! .split() method doesn't works at all in Android! StringTokenizeris working fine. – Ayush Pateria Jan 07 '12 at 17:09
  • Yes it does... what problems did you have? – Cristian Jan 07 '12 at 23:24
  • split in android receives a regular expression instead of a simple string divider. – htafoya Dec 06 '13 at 02:01
  • `String.split()` has never existed. Instead, you must call the `.split` method of any instance of `String`. So, if you have something like: `String myString = ...;` then you can do `myString.split(...)` – Cristian Dec 17 '13 at 22:42
  • How do i parse this kind of string "\/Date(1396448899247+0000)\/", what i wanted is just "1396448899247". – Uday Jun 09 '14 at 06:43
  • Without any doubt the "Split" doesn't work in all Android device! For some reason doesn't work with my BQ Aquarius and in a Sony Xperia Z1... Thank you!! – Fernando Carvalho Sep 18 '14 at 10:43
  • @AyushPateria Android split takes regular expressions so its not the same. – string.Empty Oct 07 '14 at 08:01
  • @Cristian i have String like (123) 123-1234 is my phone number and i want to get only number value for data base inserting. So how to its possible. – Hardik Parmar Jun 30 '15 at 11:00
  • @HardikParmar use regular expressions. There's plenty of information on Google. Good luck. – Cristian Jul 01 '15 at 05:21
  • 1
    @HardikParmar use `etPhoneNo.getText().toString().replaceAll("\\D", "");` its says that replace all which is not digit – MilapTank Mar 12 '16 at 10:04
  • If you're using `.split()`, pay attention to 2 things: 1 - you're passing a **regular expression** as its parameter, 2 - any **last empty sections will be discarded** unless you pass a 2nd `limit` parameter (with a value of `-1` for the most commonly expected result - see here: http://stackoverflow.com/questions/14602062/java-string-split-removed-empty-values) – Attila Tanyi Nov 06 '16 at 12:32
  • I was researching the `StringTokenizer` class and came across this... [link](https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html) "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." – tvwxyz Nov 15 '16 at 22:08
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

Community
  • 1
  • 1
Silas Greenback
  • 1,200
  • 8
  • 15
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);
}
Xavi
  • 20,111
  • 14
  • 72
  • 63
mahasam
  • 829
  • 7
  • 8
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();
Fakhar
  • 3,946
  • 39
  • 35
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.

gardarh
  • 3,084
  • 2
  • 28
  • 31
  • What's the benefit of using TextUtils.split() instead of just calling split() directly on the string? – nibarius Oct 18 '14 at 19:43
  • Edited answer to clarify difference between TextUtils.split() and String.split() – gardarh Oct 20 '14 at 10:15
  • 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. – nibarius Oct 20 '14 at 20:51
0

String s = "String="

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

add this string