문제

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.

도움이 되었습니까?

해결책 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 "

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top