Domanda

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.

È stato utile?

Soluzione 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 "

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top