-
November 16th, 2011, 03:42 AM
#1
Advance String comparison
hi
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.
Arvind
-
November 16th, 2011, 01:39 PM
#2
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,
http://download.oracle.com/javase/6/...ttern.html#sum
Last edited by nuzzle; November 17th, 2011 at 01:33 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|