• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Find String Within a String  RSS feed

 
Ranch Hand
Posts: 1936
1
  • Mark post as helpful
  • send pies
  • 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: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 1936
1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
yet simpler, try
 
lowercase baba
Posts: 12745
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Pay attention! Tiny ad!
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!