Pregunta

I have a string "10 = On Battery ", I have to split the numeric value 10 and words "On Battery"(Including the space in between them) and store it in separate variables. Please help me with some example java code.

¿Fue útil?

Solución 4

After considering your String It made me to add one thing

    String s2= "10 = On Battery ";  
    String s[]=s2.split("=");
    int i=Integer.parseInt(s[0].trim());<-----------
use trim() otherwise you will have "10 " with whitespace
                                      ^

But......

I have to split the numeric value 10 and words "On Battery"(Including the space in between them)

String a=s2.split("=")[0];//"10 "
String b=s2.split("=")[1];//" On Battery "

Otros consejos

Try like this

String originValue = "10 = On Battery";
String [] splitedValue = originValue.split("/s=/s");
System.out.println(splitedValue[0]); // 10
System.out.println(splitedValue[1]); // On Battery

Try with:

String s= "10 = On Battery ";  
String split[]=s.split("=");

split[0] will have 10 and split[1] will have On Battery

You can achieve this by using Stirng.split. Also It is very simple thing you try to learn String methods for like wise manipulation.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top