November 16th, 2011, 03:42 AM
Advance String comparison
i got stuck while comparing 2 strings. The requirement is as below:
str1 = "My name is Arvind"
str2 = "His name is Goel"
now i would like to print something like this : (Its comparing what has been changed in these strings)
"My His name is Arvind Goel"
i have used [I] and [B] here to show deleted and added characters respectively. i have used this here just for differentiating added and deleted characters. actual scenerio will be something different.
Output should have added characters in BOLD and deleted characters in Italics and rest characters as simple.
i really could not start with that how to proceed here.
Any inputs in this regard will be helpful.
Thanks for the comments.
November 16th, 2011, 01:39 PM
Re: Advance String comparison
Solutions could be pretty basic or very advanced depending on how complex strings you want to be able to handle.
A simple solution would be to just split both strings at word boundaries to get the individual words. Then you compare the words pairwise. If they're different you print both words after each other (indicating in some way which string each word belongs to) and if they're equal you just print one word.
One way to split a String is to use the split method. It goes something like this,
String str1Array = str1.split("\\b");
The array holds all substrings of str1 after it's been split at word boundaries as defined by the \b regular expression,
Last edited by nuzzle; November 17th, 2011 at 01:33 AM.
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.