• 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
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Find String Within a String

 
Ranch Hand
Posts: 2298
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a string like:

1,7,8 or

7,8,11

If I use the indexOf("1") it will find a match in both strings. How can I make it do an exact match?
 
author and iconoclast
Posts: 24208
47
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
You just have to be more specific about what an exact match would entail. You could use a regular expression which said something like

a comma or the beginning of the string, followed by
the search target, followed by
a comma or the end of the string.
 
Steve Dyke
Ranch Hand
Posts: 2298
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if my string is:

11,

indexOf(",11,") will not work.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, but building a regular expression and and using matches() will:
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yet simpler, try
 
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
O'Reilly has entire books on writing regular expressions. you may want to check some of them out.

part of the art of writing a good regex is understanding what you input can be, as much as what you want to get out of it...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guido's last post is good for a simple way to do this without regular expressions. However if you find you need a more flexible solution, you may need to come back to regular expressions. Good free tutorials on regular expressions can be found at

http://www.regular-expressions.info/tutorial.html

and

http://java.sun.com/docs/books/tutorial/essential/regex/

Also of interest is the API for the Pattern class.

In this case you can also make use of \b to match a word boundary: "\\b11\\b" will match "11" as long as it's preceded and followed by non-word characters, or the beginning or end of the string.
[ April 30, 2008: Message edited by: Jim Yingst ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic