Forum: Ruby how to find difference between two strings

Posted by Priyank Shah (pshah)
on 2010-07-30 15:17
Hi,

I have two strings, like
@a = "hello"
@b = "h123llo"

so in that how can i print diffrence?

i want difference like user know which diffrence on which character.

Please let me know if any one know.

Thanks in advance
Priyank
Posted by Peter Hickman (Guest)
on 2010-07-30 15:34
(Received via mailing list)
One method, and I stress this is only one method, is the Levenshtein 
distance at

http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Levenshtein_distance#Ruby

and it has code, in ruby!
Posted by Josh Cheek (Guest)
on 2010-07-30 20:44
(Received via mailing list)
On Fri, Jul 30, 2010 at 8:17 AM, Priyank Shah 
<shahpriyank01@gmail.com>wrote:

> Please let me know if any one know.
>
> Thanks in advance
> Priyank
> --
> Posted via http://www.ruby-forum.com/.
>
>
"Difference" in this case does not seem very well defined.
Posted by Thomas Sawyer (7rans)
on 2010-07-30 21:29
(Received via mailing list)
On Jul 30, 9:17 am, Priyank Shah <shahpriyan...@gmail.com> wrote:
> Hi,
>
> I have two strings, like
> @a = "hello"
> @b = "h123llo"
>
> so in that how can i print diffrence?
>
> i want difference like user know which diffrence on which character.

There is strmask (http://rubyworks.github.com/strmask/)

Also the Levenshtein distance is  String#edit_distance in Ruby Facets.
Posted by Gavin Sinclair (Guest)
on 2010-07-31 00:36
(Received via mailing list)
> 
> i want difference like user know which diffrence on which character.
> 
There's a "differ" gem which could be useful for you.

Gavin
Posted by Priyank Shah (pshah)
on 2010-07-31 07:11
Priyank Shah wrote:
> Hi,
> 
> I have two strings, like
> @a = "hello"
> @b = "h123llo"
> 
> so in that how can i print diffrence?
> 
> i want difference like user know which diffrence on which character.
> 
> Please let me know if any one know.
> 
> Thanks in advance
> Priyank



Thanks to you all but i want difference so i used

http://codeidol.com/other/rubyckbk/Files-and-Directories/Comparing-Two-Files/

And its working fine but i actully want exact difference.

and it just shows me number of differences.

So if you have any idea regarding this then please suggest.

Thanks,
Priyank Shah
Posted by Thomas Sawyer (7rans)
on 2010-07-31 08:11
(Received via mailing list)
On Jul 31, 1:11 am, Priyank Shah <shahpriyan...@gmail.com> wrote:
>
>
> and it just shows me number of differences.
>
> So if you have any idea regarding this then please suggest.

As in:

   @b.split(//) - @a.split(//)

?
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.