• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finding a sub-string within a string

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why wouldn't indexOf() work?
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The api tells you that if the substring is not found, it will return a value of -1.
 
On top of spaghetti all covered in cheese, there was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic