10

I have a string that has the value of name:score. I want to split the string into two strings, string a with the value of name and string b with the value of score.

What is the correct function/syntax to do this?

I have looked at string.split, but can not find the actual syntax to return the data into two separate strings.

2
  • 1
    Did you look at the return value from split()? Commented Oct 16, 2011 at 21:18
  • 1
    Nevermind, I am an idiot. The values were there the whole time in variable[0] and variable[1]. Commented Oct 16, 2011 at 21:22

7 Answers 7

32

The split function is suitable for that :

String[] str_array = "name:score".split(":");
String stringa = str_array[0]; 
String stringb = str_array[1];
Sign up to request clarification or add additional context in comments.

3 Comments

what if you have something like this a:1:2 name = a:1?!?@
It will return an array of strings and will be able to retrieve the 1st and 2nd value
This code only works if your score does not contain a colon. Be sure to account for this in your code.
6

You need to look into Regular Expressions:

String[] s = myString.split("\\:"); // escape the colon just in case as it has special meaning in a regex

Or you can also use a StringTokenizer.

3 Comments

: doesn't have any special meaning on its own
Never hurts to be safe and escape it... /a{1:2}/ is a case when colon has a special meaning...
@TraderJoeChicago: No, it doesn't. You're probably talking about a{1,2} which matches a 1 or 2 times. A colon doesn't have any special meaning on its own.
2

Use:

String [] stringParts = myString.split(":");

3 Comments

But isn't that just creating a new array without moving the data into the other strings?
@CryptoJones Yes, it is creating a new array of Strings with 2 strings, name and score, which is what you want.
@CryptoJones - My guess is that split returns substrings of the original string and does not actually move any character data. But you shouldn't care. String objects are immutable in Java, and their internal data are inaccessible to your code, so it can't make any difference in what you do later.
2
String row = "name:12345";
String[] columns = row.split(":");
assert columns.length == 2;
String name = columns[0];
int score = Integer.parseInt(columns[1]);

Comments

1

Split creates an array with your strings in it:

String input = "name:score";
final String[] splitStringArray = input.split(":");
String a = splitStringArray[0];
String b = splitStringArray[1];

Comments

1

$ cat Split.java

public class Split {
    public static void main(String argv[]) {
        String s = "a:b";
        String res[] = s.split(":");
        System.out.println(res.length);
        for (int i = 0; i < res.length; i++)
            System.out.println(res[i]);
    }
}

$ java Split

2
a
b

Comments

1

what if you have something like this a:1:2 name = a:1??

 private String extractName(String str) {
    String[] split = str.split(":");
    return str.replace(split[split.length - 1], "");
  }

  private int extractId(String str){
    String[] split  = str.split(":");
    return  Integer.parseInt(CharMatcher.DIGIT.retainFrom(split[split.length-1]));
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.