Isolate 2nd or 3rd instance of .indexOf(something)
Coding Style: NetBeans IDE 4.0 Beta2 (Java)
Given the following string
String Info = "NAME:ME IP:192.168.0.1 PORT:4444"
I wanted to extract the information from the string using .substring and .indexOf, specifically .Substring(.indexOf("NAME:")+5, .indexOf(" "));
That works GREAT however accessing the rest won't (outofbounds errors): .subString(.indexOf("IP:")+3, .indexOf(" "))
This fails (I think) because my .indexOf(" ") is returning the index of the 1st space (after NAME:ME) and not the next one after IP:192.168.0.1
So how can I make .indexOf jump to the NEXT (" ") character? Or is there a better way to do this?
Any help would be appreciated, Thanks,
Why don't you use a string tokenizer and just enumerate through the resultant collection?
StringTokenizer is deprecated. The API advises to use String.split() instead. You can see how to use the method here...http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)
I think you may be best splitting the string on the spaces as follows...
stringArray will then be an array of String objects containing
String stringArray = yourString.split(" ");
NAME:ME - index 0
IP:192.168.0.1 - index 1
PORT:4444 - index 2
Shows the last time I used Java
Anyway, Split() has been in VB since VB6 and its excellent
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center