Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting String into Array of Strings Java

I have a String that looks like this

The#red#studio#502#4

I need to split it into 3 different Strings in the array to be

s[0] = "The red studio"
s[1] = "502"
s[2] = "4"

The problem is the first one should have only words and the second and third should have only numbers...

I was trying to play with the s.split() Method, but no luck.

like image 524
Andrey Chasovski Avatar asked Jan 28 '26 04:01

Andrey Chasovski


1 Answers

String s= "The#red#studio#502#4";
String[] array = s.split("#(?=[0-9])");
for(String str : array)
{
  System.out.println(str.replace('#',' '));
}

Output:

The red studio  
502  
4  

Ideone link.

like image 189
Srinivas Avatar answered Jan 30 '26 00:01

Srinivas