# [java] Comparing 2 strings, case problem

This topic is 4846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi folks. Im currently in the needs of comparing two string. The deal is, one of the strings is just a word, the other might be a full phrase, with many occurrences of the first word on it. I basically need to replace all ocurrences of the first word in the second one with a third word (think of it as a "bad word" with a replacement), but dont know how to do it, since I have the restriction that the case must stay as-is. What I meant is that, when I make the replacements, the case must be left like it was originally, so Ill give you guys an example: Word: Foo (Bad word - Faulty language) Replacement: Nice Phrase: foO is SO Cool, IM Totally Into Foo 24/7 Re-made Phrase with replacement: Nice is SO Cool, IM Totally Into Nice 24/7 You see? I cant just use a replaceAll() and prior to it make both Strings lowercase to make a comparison, since then I'd have a re-made phrase without original case, like this Nice is so cool, im totally into Nice 24/7 Thanks for your time

##### Share on other sites
Store the position of the upper cased chars in each word. Then you go through each replacement word looking for the character in the indexed position, turning it to lower case.

And when the lenght of the replacement string differs from the bad string, it will be impossible to respect the casing of the string all times.

Son Of Cain

##### Share on other sites
Just use StringTokenizer to get individual words and replace them as needed. Or am I missing something?

##### Share on other sites
Regular expressions would be an easy way to go..

String phrase = "foO is SO Cool, IM Totally Into Foo 24/7";String original_word = "Foo";String replacement_word = "Nice";Matcher matcher = new Matcher();matcher.usePattern(new Pattern(Pattern.quote(original_word), Pattern.CASE_INSENSITIVE));String corrected_phrase = matcher.replaceAll(phrase);

You could also do this:
1) make a lower-case copy of 'phrase' for matching, hang on to the original
2) use indexOf() against the lower-case version to find the target word
3) use that index to replace the target word in the *original* string
4) repeat

##### Share on other sites
Quote:
 Original post by pinacoladaYou could also do this:1) make a lower-case copy of 'phrase' for matching, hang on to the original2) use indexOf() against the lower-case version to find the target word3) use that index to replace the target word in the *original* string4) repeat

Well, I just did this a couple of minutes ago, but doesnt seem to be too optimal (or probably I didnt understood you well). Its all about switching indexes and a for loop, but probably that solution would earn me a Java Hall of Shame 2005 award.

Anyways, your regex solution seems just what Im looking for. I was sure I could do wonders with regex, but since the javadocs expect you to be a master of regular expressions to work with it, then I didnt dare to touch that. Thanks a lot.

As for the Anonymous Poster, and Son of Cain, thanks a lot too folks for your concern.

Cheers ya'll

##### Share on other sites
Oh hang on, my code has some problems. Sorry, this is all untested. But this snippet might work:

String phrase = "foO is SO Cool, IM Totally Into Foo 24/7";
String original_word = "Foo";
String replacement_word = "Nice";

Pattern pattern = new Pattern(Pattern.quote(original_word), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.match(phrase);
String corrected_phrase = matcher.replaceAll(replacement_word);

##### Share on other sites
Thanks again dude. I was just putting my fingers over the ctrl+c when I said "Lets refresh the page" [grin]

Cheers

##### Share on other sites
Quote:
 Original post by pinacoladaOh hang on, my code has some problems. Sorry, this is all untested. But this snippet might work:String phrase = "foO is SO Cool, IM Totally Into Foo 24/7";String original_word = "Foo";String replacement_word = "Nice";Pattern pattern = new Pattern(Pattern.quote(original_word), Pattern.CASE_INSENSITIVE);Matcher matcher = pattern.match(phrase);String corrected_phrase = matcher.replaceAll(replacement_word);
Wouldn't that be:
String phrase           = "foO is SO Cool, IM Totally Into Foo 24/7";
String original_word    = "Foo";
String replacement_word = "Nice";

Pattern pattern         = Pattern.compile(Pattern.quote(original_word), Pattern.CASE_INSENSITIVE);
Matcher matcher         = pattern.match(phrase);
String corrected_phrase = matcher.replaceAll(replacement_word);

##### Share on other sites
Quote:
 Original post by lucky_monkeyWouldn't that be:...

Actually, it would be

String phrase = "foO is SO Cool, IM Totally Into Foo 24/7";
String original_word = "Foo";
String replacement_word = "Nice";

Pattern pattern = Pattern.compile(Pattern.quote(original_word), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(phrase);
String corrected_phrase = matcher.replaceAll(replacement_word);

But thats just "polishing" an untested snippet. Now I have to say it works like a real charm!
I probably should look into Java regex more deeply now.

##### Share on other sites
[smile]
Yeah, regular expressions are good.

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633330
• Total Posts
3011388
• ### Who's Online (See full list)

There are no registered users currently online

×