Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Search Using Java....

 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I am trying to search a text file using JAVA. But my problem is i need exact matches of the occurrences. That is not happening. I am trying with contains() method to search for the string. For example...

I have a file which contains

John is good boy.
Marie is a good girl.
Jack is good boy.
Johnny is good boy.
Johnson is good boy.

I am using the search string as John.

I need the output as :
John is good boy.

The problem is I am getting the output as:

John is good boy.
Johnny is good boy.
Johnson is good boy.

Coz they all contain John. I want the out just as:
John is a good boy.

Is there any way to match the exact string.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick solution will be,

1. Get a line as a string.
2. Use String#split(" ") OR StringTokenizer class, and get a string array on "sapce", so that you get words separated.
3. Match for the occurrence on every array element.
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please provide some of the syntax for the process.

[ UD: Please start a new thread for questions that are unrelated to the original topic. ]
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somnath Mallick wrote:Could you please provide some of the syntax for the process.

Whatever you're looking for, it is at JavaDocs
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of searching for "John" couldn't you just search for "John " <-- The space will only be present for the selection you are looking for. The other method would be as Sagar mentioned to use a String Tokenizer. Creating tokens would most likely be your best bet
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could set up a regular expression; you can set up tags for "word boundary" so you put John between two of those tags. I have forgotten what you use for the tags, but it will all be in the link I gave you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic