Two Laptop Bag
The moose likes Beginning Java and the fly likes substring(IndexOf( )) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "substring(IndexOf( ))" Watch "substring(IndexOf( ))" New topic

substring(IndexOf( ))

Bart Wilson

Joined: Aug 01, 2001
Posts: 17
Is this correct? substring not subString, right?
I want strLastName to only equal the characters that precedes a comma (,):
strLastName = name.substring(0,name.indexOf(",")
This would extract characters after the comma to the end of the index:?
strFirstName = name.substring(name.indexOf(","))
namita pa
Ranch Hand

Joined: Sep 06, 2001
Posts: 56
To use StringTokenizer is the better approach
StringTokenizer st=new StringTokenizer(s,",");
where s is your string
this will break the string depending on the delimiter provided "," in this case
now you can do
String s1=st.nextToken();
s1 is the required string
i hope that it would work fine
all the best
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Yes Bart, you've got it right, but with one small difference.

There is a slight difference in how substring uses the index you pass it, when you use substring(int, int) and substring(int)

Parsing the last name will work nicely because substring with two parameters will start at the first index and go to "the last index minus one" (so the comma is not included).

Parsing the first name is not as good, because substring with one parameter will start at the index you give it (which in this case, is the comma).
So if you want the first name, you have to remember to add 2 to the returned value from indexOf(",") (one to get past the comma and one to get past the space that I'm assuming is there).

namita has a good idea with the Tokenizer, but what about that space? You could include the space in your token String (", "), but then what if someone has a name like "Smith, Mary Jane". The Tokenizer would return three tokens in this case. So even if you used the Tokenizer, you'd probably have to snip off spaces yourself.

And hey.. there's even a handy API method for that too. trim()
[This message has been edited by Mike Curwen (edited September 07, 2001).]
Bart Wilson

Joined: Aug 01, 2001
Posts: 17
What do mean to add 2? Is this why I am being thrown a catch error? I edited the substring(0,indexOf(",")) to
substring(0, indexOf(",", -1)) to make it search from -1, didn't work still had a catch error.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

What's a catch error? If you're wanting to say that an error is thrown, just say "It throws the following error: " and then give us a stack trace or the name of the error (NullPointerException) for example.

Don't bother sending in -1, if you just use the single parameter version of indexOf(), it starts from the beginning.

What I meant by "add 2"
If I didn't add 2 at line 4, the output at line 8 would be
Because indexOf(",") returns (in this example) 6, but my first name doesn't start until position 8.
I agree. Here's the link:
subject: substring(IndexOf( ))
It's not a secret anymore!