Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split Java String into Two String using delimiter

Tags:

java

string

regex

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.

like image 678
CryptoJones Avatar asked Oct 16 '11 21:10

CryptoJones


2 Answers

The split function is suitable for that :

String[] str_array = "name:score".split(":");
String stringa = str_array[0]; 
String stringb = str_array[1];
like image 176
Dimitri Avatar answered Sep 26 '22 00:09

Dimitri


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.

like image 38
TraderJoeChicago Avatar answered Sep 25 '22 00:09

TraderJoeChicago