aspose file tools*
The moose likes Beginning Java and the fly likes Finding a sub-string within a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finding a sub-string within a string" Watch "Finding a sub-string within a string" New topic
Author

Finding a sub-string within a string

Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
i have a String s1. user is queried to enter a sub-String sSub.
i need to see if sSub exists within s1.
eg;,
s1 = "JAVA programming is wonderful";
sSub = "AVA"
if sSub exists in s1 return "TRUE else return false;
result is boolean and MUST be case sensitive.
i see lastIndexOf() and indexOf() but am really looking for something like
anyIndexOf().
so how do you perform this little trick?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

Why wouldn't indexOf() work?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
indexOf() and lastIndexOf() are the best solutions for you problem, i really have to wonder why you don't want to use them.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
Thanks for the input.
I am not averse to using them just wondering if there was another method?
What happens with s1.indexOf(sSub) when sSub is not part of s1?
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by Douglas Braxton:
Thanks for the input.
I am not averse to using them just wondering if there was another method?
What happens with s1.indexOf(sSub) when sSub is not part of s1?

Negative value is returned.
Joe Gilvary
Ranch Hand

Joined: May 11, 2001
Posts: 152
One way would be to move through the String checking the 3 character substring that begins in index 0, then the one that begins in index 1, then the one that begins in index 2, and so on.
Another way would be to move through the String looking for the first letter of the substring, and if you find it, compare the next index in the string to the second character of the substring, and so on.
There are lots of ways to do something like this. I hope one of these suggestions helps you get started.
Thanks,
Joe
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
What Joe has said is correct and can be considered as a solution, but reinventing the wheel is never a good idea.

Use the wheel, don�t reinvent it
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

The api tells you that if the substring is not found, it will return a value of -1.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finding a sub-string within a string