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.
Asked
Active
Viewed 496 times
-6

tod
- 1,539
- 4
- 17
- 43

user3611810
- 47
- 7
-
2this question really lacks research effort.. Did you search SO before asking? – sanbhat May 07 '14 at 10:47
-
2**Surprise....!!!** You question contains answer!!! :) – akash May 07 '14 at 10:47
-
1Your question lacks any effort. – tod May 07 '14 at 10:50
4 Answers
0
Try with:
String s= "10 = On Battery ";
String split[]=s.split("=");
split[0]
will have 10 and split[1]
will have On Battery

Rajesh Omanakuttan
- 6,788
- 7
- 47
- 85

SpringLearner
- 13,738
- 20
- 78
- 116
-
1And then you can do Integer.parseInt(split[0]) in order to get it as an integer value. – flotothemoon May 07 '14 at 10:49
0
You can achieve this by using Stirng.split
. Also It is very simple thing you try to learn String methods for like wise manipulation.

Macrosoft-Dev
- 2,195
- 1
- 12
- 15
-2
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 "

akash
- 22,664
- 11
- 59
- 87
-
" On Battery " is prefixed and suffixed with spaces. Can you tell me a way to remove those spaces without affected space between On and Battery. – user3611810 May 07 '14 at 11:17
-
I answered that use `trim()` it will remove extra spaces prefix and suffix as well – akash May 07 '14 at 11:18