Question

I have a string "552 s hello" I want to get the "552" in one substring and "s" in other substring and I want to split this String on the basis of first two spaces.Please help...

Was it helpful?

Solution 2

Use String#split(String regex):

 String[] sp="552 s hello".split(" ");
 String fiveFiveTwo=sp[0];
 String letterS=sp[1];

The names aren't very useful in the end, so you'll want to rename the strings I've created. You should also catch ArrayIndexOutOfBoundsException.

OTHER TIPS

You can do it like this:

String t = "522 s hello"
String[] test = t.split(" ");

Now in the String array you will have three values, "522", "s" and "hello. And you can now access each.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top