File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes search string from bigger string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "search string from bigger string" Watch "search string from bigger string" New topic

search string from bigger string

Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
I have a big string. For example:
bigString="This is big string. Big string is not small";
and i have small string:
and i have to find how many matches with small string i have in big one.
As a result there should be 2 matches. I have searched the whole string class doc and nothing simple has not come to my mind - it can't be hard.
So does anyone know any good methods to find a match of one string to another?
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
Is it your home work obviously ?
Something like this:

[ December 01, 2003: Message edited by: Igor Ko ]
Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
Yes - it's my homework at school and i'm really out of time. I remember the teacher showd this in class and it was so obvious that i didn't write it down - now i'm in deep ...
I didn't understand the code quite clearly - so if u could add sine comments it would be nice.
Allso ... String.regionMatches() - i don't understand what are the parameters for this fx and can i use it?
ok thnx - need more help.
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
Take a look at these examples.

Output is 3. Matches all occurances of "is" even the one in "this"

Output is 2.
But if bigstring had contained something like "Do not end a sentence with the word is." it would have returned 0 because of the fullstop.
By using regular expressions we can get around that:

Returns 3. Similar to the first attempt - trying to match "is" also matches "this"

Returns 2
Zeeman Chen

Joined: Oct 30, 2003
Posts: 9
I'm a newbie, I have a question I'd like to ask,
Is empty for loop " for(; a good programming style?

If a man take no thought at what is distant, he will find sorrow near at hand. ~ Confucius
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

I would not use an empty for loop. I use a for loop when i know (or can calculate) the number of iterations i need.
if i want an infinate loop, with a break condition, i'd use

Better/worse/proper is a debate that will go on forever...

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
John Guilliani

Joined: Oct 11, 2003
Posts: 4
Is there a way to count the number of times the smallString is found in reverse order as well? For example if the BigString = "Mississipi" and the SmallString = "is". The number of occurrences would be 4. Counting the occurrences of "si" as well.
I agree. Here's the link:
subject: search string from bigger string
jQuery in Action, 3rd edition