Jump to content
  • Advertisement
Sign in to follow this  
darklordsatan

[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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just use StringTokenizer to get individual words and replace them as needed. Or am I missing something?

Share this post


Link to post
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 this post


Link to post
Share on other sites
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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!