CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: Advance String comparison

  1. #1
    Join Date
    Jul 2011

    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.

  2. #2
    Join Date
    May 2009

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)