java - How Do I separate String from Integer and Save it



I wanted to know how can I separate a string into two parts and save it in two different variables.

I have:

String str = "3-abc";

And want to save it in two Strings:

String part1 = "3";
String part2 = "abc";

Any help will be highly appreciated, Thanks

4 Answers: 

String[] strArray = str.split("-");
String part1=strArray[0]; 
String part2=strArray[1];

You can use split function

String[] temp;
String delimiter = "-";
temp = str.split(delimter);
for(int i =0; i < temp.length ; i++)

You can use the split method of String class. So

String[] parts = str.split("-");

String part1 = parts[0];
String part2 = parts[1];

From Java Documentation:

Splits this string around matches of the given regular expression. 

    the array of strings computed by splitting this string around 
    matches of the given regular expression 

If strings are all in one format you can use String[] splittedStrings = str.split("-"); After that try to convert your string to integer with Integer.parseInt(splittedStrings[0]);