The Artima Developer Community
Sponsored Link

Java Answers Forum
Isolate 2nd or 3rd instance of .indexOf(something)

1 reply on 1 page. Most recent reply: Apr 7, 2005 12:58 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Shaitan

Posts: 11
Nickname: shaitan00
Registered: Feb, 2005

Isolate 2nd or 3rd instance of .indexOf(something) Posted: Apr 7, 2005 12:34 AM
Reply to this message Reply
Advertisement
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,


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Isolate 2nd or 3rd instance of .indexOf(something) Posted: Apr 7, 2005 12:58 AM
Reply to this message Reply
from the String API:
public int indexOf(String str, int fromIndex)

fromIndex is the start posision, where the search starts.
//This gives you the 2nd position:
s.indexOf(" ", s.indexOf(" ") + 1)
//For the 3rd position:
s.indexOf(" ", s.indexOf(" ", s.indexOf(" ") + 1) +1)[//java]

Flat View: This topic has 1 reply on 1 page
Topic: Problem Creating Executable JAR Previous Topic   Next Topic Topic: Layoutmanager

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use