Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding a sub-string within a string

 
Douglas Braxton
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why wouldn't indexOf() work?
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
indexOf() and lastIndexOf() are the best solutions for you problem, i really have to wonder why you don't want to use them.
 
Douglas Braxton
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic