9 Replies - 4890 Views - Last Post: 18 March 2009 - 09:26 AM Rate Topic: -----

#1 dom96  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 29-December 08

Difference between two strings

Posted 15 March 2009 - 09:31 PM

hi, i need to be able to get the difference between two strings, i found out how to get the difference with this code:
Dim Changes As String = NewText.Replace(OldText, " ")


but i also need to be able to find the index of where the difference is, does anybody know how to do that ?
EDIT:I just found a flaw in that code, if the OldText is "html" and the NewText is "html html" Changes is not gonna be "html"

This post has been edited by dom96: 15 March 2009 - 09:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Difference between two strings

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Difference between two strings

Posted 15 March 2009 - 10:35 PM

If you are wanting to find the index of each item that matches the replace string then you can simply use the index property of the Match object. Here is an example...

' Create a regular expression looking for "HTML" in string
Dim rx As New Regex("HTML", RegexOptions.Compiled Or RegexOptions.IgnoreCase)

' Setup our string, notice two matches
Dim str As String = "HTML <p> tag or HTML <div> tag"

' Test the string and create a collection of match objects
Dim matches As MatchCollection = rx.Matches(str)

' Loop through the matches showing a messagebox to display their position
For Each match As Match In matches
     MessageBox.Show(match.Index)
Next



The code above will print two messageboxes, 0 and 16 which is the index of both matches in the string. You can use this to find the number of matches, where they are in the string and then of course you can do your replacement using a Regex.Replace() method.

Hope this solves the problem for you. Enjoy!

p.s. Make sure you import the System.Text.RegularExpressions namespace to get access to the Regex and match objects.

"At DIC we be regular expression finding and replacing code ninjas... we will find and replace you, count on it!" :snap:

This post has been edited by Martyr2: 15 March 2009 - 10:36 PM

Was This Post Helpful? 0
  • +
  • -

#3 dom96  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 29-December 08

Re: Difference between two strings

Posted 16 March 2009 - 04:43 AM

View PostMartyr2, on 15 Mar, 2009 - 09:35 PM, said:

If you are wanting to find the index of each item that matches the replace string then you can simply use the index property of the Match object. Here is an example...

' Create a regular expression looking for "HTML" in string
Dim rx As New Regex("HTML", RegexOptions.Compiled Or RegexOptions.IgnoreCase)

' Setup our string, notice two matches
Dim str As String = "HTML <p> tag or HTML <div> tag"

' Test the string and create a collection of match objects
Dim matches As MatchCollection = rx.Matches(str)

' Loop through the matches showing a messagebox to display their position
For Each match As Match In matches
     MessageBox.Show(match.Index)
Next



The code above will print two messageboxes, 0 and 16 which is the index of both matches in the string. You can use this to find the number of matches, where they are in the string and then of course you can do your replacement using a Regex.Replace() method.

Hope this solves the problem for you. Enjoy!

p.s. Make sure you import the System.Text.RegularExpressions namespace to get access to the Regex and match objects.

"At DIC we be regular expression finding and replacing code ninjas... we will find and replace you, count on it!" :snap:

hi, and thanks for the reply, but i have to look for differences they're not always just "HTML", i could just use my code to find the differences and match them using regex, but then if there is for example like in your code two "HTML" then if i add "HTML" to the new string then how would i know which ones which ?
Was This Post Helpful? 0
  • +
  • -

#4 dom96  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 29-December 08

Re: Difference between two strings

Posted 17 March 2009 - 02:25 AM

Any other ideas ?
Was This Post Helpful? 0
  • +
  • -

#5 dom96  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 29-December 08

Re: Difference between two strings

Posted 17 March 2009 - 04:31 PM

please somebody ...
Was This Post Helpful? 0
  • +
  • -

#6 DeCompile  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 301
  • Joined: 20-July 08

Re: Difference between two strings

Posted 17 March 2009 - 05:24 PM

Would something like:

Dim stringone As String = TextBox1.Text
		Dim stringtwo As String = TextBox2.Text

		For i = 1 To stringone.Length

			If Mid(stringtwo, i, 1) <> Mid(stringone, i, 1) Then

				TextBox3.Text += Mid(stringtwo, i, 1)

			End If

		Next


Suffice?

This post has been edited by DeCompile: 17 March 2009 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

#7 dom96  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 29-December 08

Re: Difference between two strings

Posted 17 March 2009 - 05:41 PM

View PostDeCompile, on 17 Mar, 2009 - 04:24 PM, said:

Would something like:

Dim stringone As String = TextBox1.Text
		Dim stringtwo As String = TextBox2.Text

		For i = 1 To stringone.Length

			If Mid(stringtwo, i, 1) <> Mid(stringone, i, 1) Then

				TextBox3.Text += Mid(stringtwo, i, 1)

			End If

		Next


Suffice?

Thanks a lot for your reaply, but it doesn't work very well, if the beggining of stringone and stringtwo is the same it doesn't show the difference
Was This Post Helpful? 0
  • +
  • -

#8 DeCompile  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 301
  • Joined: 20-July 08

Re: Difference between two strings

Posted 17 March 2009 - 05:47 PM

I think you may need to check your objective.

Quote

the beggining of stringone and stringtwo is the same it doesn't show the difference


If the beginning of both of both strings are the same, then there is no difference.
Was This Post Helpful? 0
  • +
  • -

#9 myork  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 129
  • Joined: 04-March 09

Re: Difference between two strings

Posted 17 March 2009 - 08:19 PM

I think this will do what you're looking for (If not, you can always modify it from here)
	Public Enum DifferenceParam
		NULL
		Ignore_Case
		Ignore_Internal_Words
	End Enum

	Public Function stringDifference(ByVal str1 As String, ByVal str2 As String, Optional ByVal param As DifferenceParam = DifferenceParam.NULL, Optional ByVal param2 As DifferenceParam = DifferenceParam.NULL, Optional ByVal param3 As DifferenceParam = DifferenceParam.NULL) As String
		If str1 = str2 Then Return ""
		Dim spaces As String = ""

		If param = DifferenceParam.Ignore_Case OrElse param2 = DifferenceParam.Ignore_Case OrElse param3 = DifferenceParam.Ignore_Case Then
			str1 = str1.ToLower
			str2 = str2.ToLower
		End If
		If param = DifferenceParam.Ignore_Internal_Words OrElse param2 = DifferenceParam.Ignore_Internal_Words OrElse param3 = DifferenceParam.Ignore_Internal_Words Then
			spaces = " "
		End If

		Dim bigger As String
		Dim smaller As String

		If str2.Length > str1.Length Then
			bigger = str2
			smaller = str1
		Else
			bigger = str1
			smaller = str2
		End If

		If bigger.Contains(spaces & smaller & spaces) Then
			Return bigger.Remove(bigger.IndexOf(spaces & smaller & spaces) + spaces.Length, smaller.Length + spaces.Length)
		ElseIf bigger.Contains(spaces & smaller) Then
			If bigger.IndexOf(spaces & smaller) = bigger.Length - smaller.Length Then
				Return bigger.Remove(bigger.Length - smaller.Length)
			Else
				Return "\NULL"
			End If
		ElseIf bigger.Contains(smaller & spaces) Then
			If bigger.IndexOf(spaces & smaller) = 0 Then
				Return bigger.Remove(0, smaller.Length)
			Else
				Return "\NULL"
			End If
		Else
			Return "\NULL"
		End If
	End Function

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Console.WriteLine(stringDifference("There is a fly in my soup, Sir", "in"))
		Console.WriteLine(stringDifference("There is a fly in my soup, Sir", "in", DifferenceParam.Ignore_Case))
		Console.WriteLine(stringDifference("In my soup there is a fly, Sir", "in"))
		Console.WriteLine(stringDifference("In my soup there is a fly, Sir", "in", DifferenceParam.Ignore_Case))
		Console.WriteLine(stringDifference("There is a fly in my soup, Sir", "in", DifferenceParam.Ignore_Internal_Words))
		If (stringDifference("String", "nothing here") = "\NULL") Then Console.WriteLine("Success")
		If (stringDifference("StringDup", "StringDup") = Nothing) Then Console.WriteLine("Success")
		If (stringDifference("StringDup", "Stringdup", DifferenceParam.Ignore_Case) = Nothing) Then Console.WriteLine("Success")
		Console.WriteLine(stringDifference("StringDup", "StringDup2"))
		If (stringDifference("StringDup", "StringDup2", DifferenceParam.Ignore_Internal_Words) = "\NULL") Then Console.WriteLine("Success")
		Me.Close()
	End Sub



and the console dump is
There is a fly  my soup, Sir
there is a fly  my soup, sir
\NULL
 my soup there is a fly, sir
There is a fly my soup, Sir
Success
Success
Success
2
Success



There's a couple spots it can be improved, but I'll leave that up to you :)

\NULL is returned if the strings don't have the smaller string in common, Nothing, or String.Empty, or "" is returned if they're the same.

This post has been edited by myork: 17 March 2009 - 08:21 PM

Was This Post Helpful? 1
  • +
  • -

#10 dom96  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 29-December 08

Re: Difference between two strings

Posted 18 March 2009 - 09:26 AM

That's a good piece of code, it doesn't give the index's and yeah it does have some problems, but i'll try to fix those myself(hopefully i'll succed) thanks a lot
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1