• 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

Strings contained in strings

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the best way to go about checking if a String being searched for is contained within another String (ignoring case).

ex) Cat is contained in Catastraphy

I searched the string class online but found no functions that might help me do this. Maybe just using indexOf() with a string parameter, because that way if I don't get back an index it's not there, and if I do it is there? I'm trying to keep a record of which strings contain other strings, like all the strings in a list that contain the word "Cat" for example. Thanks
 
Greenhorn
Posts: 25
MySQL Database PHP Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

You have to use regular expressions...

look for...

String.matches()

and

Pattern Class, to understand Regular Expressions
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using indexOf() this way is common and a fine thing to do; but it will only find exact matches, including letter case. A more general solution would be to use regular expressions -- String.matches() or its more general form java.util.regex.Pattern.matches(). Something like

// Keep "p", use for all your Strings to check if they contain "cat"
Pattern p = Pattern.compile(".*cat.*", Pattern.CASE_INSENSITIVE);

// For each string you want to match
Matcher m = p.matcher(theString);
boolean containsCatIgnoringCase = m.matches();
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also try doing:



Then test the value of i to see if "cat" existed in the original string.
 
Marshal
Posts: 79232
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Ubuntu Climber," please check the JavaRanch naming policy and adjust your displayed name accordingly.
 
reply
    Bookmark Topic Watch Topic
  • New Topic