[java] Comparing 2 strings, case problem

Started by
8 comments, last by lucky_monkey 18 years, 8 months ago
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
Advertisement
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
a.k.a javabeats at yahoo.ca
Just use StringTokenizer to get individual words and replace them as needed. Or am I missing something?
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
Quote:Original post by pinacolada
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

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

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);
Thanks again dude. I was just putting my fingers over the ctrl+c when I said "Lets refresh the page" [grin]

Cheers
Quote:Original post by pinacolada
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);
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);
Quote:Original post by lucky_monkey
Wouldn'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.

[smile]
Yeah, regular expressions are good.

This topic is closed to new replies.

Advertisement