-1

I'm trying to delete the common characters between two strings. My problem is that the for each loop is not deleting the correct characters between strname and strname2.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button3.Click
Dim strname, strname2 As String
Dim r As Double
strname = TextBox1.Text
strname2 = TextBox2.Text
For Each c As Char In strname
    If (strname2.IndexOf(c) > -1) Then
        strname2 = strname2.Remove(strname2.IndexOf(c), 1)
        strname = strname.Remove(strname.IndexOf(c), 1)
    End If
Next
Dim result As Double = strname.Length + strname2.Length
Label6.Text = result
Flexo
  • 87,323
  • 22
  • 191
  • 272
  • When you remove the character from your strings, do you want to remove all occurrences of the character or just the first occurrence? If the way you have it written worked, it would only remove the first occurrence. – j.f. Oct 16 '14 at 13:03
  • yes that is exactly my problem can you help me with that i just figured it out that when i enter a specific string example: string 1 = dad string 2 = amm the code will delete nothing because string1's a is in index 1 while string 2's a is index 0 – anitstudent Oct 16 '14 at 13:11
  • Yep, good catch. That will definitely be a problem. If you want to remove all occurrences, I would suggest using `Replace()` to replace the character(s) with an empty string. – j.f. Oct 16 '14 at 13:15
  • can you explain it more how am i supposed to know which string i would replace thanks in advance – anitstudent Oct 16 '14 at 13:34
  • @j.f. when editing please don't put (or keep) answers in the question itself. – Flexo Nov 12 '14 at 09:16

1 Answers1

0

If I understand your requirements correctly, what you need to do is find each character in string1 that exists in string2. For everyone character you find, remove all occurrences of it from both strings.

One option is to just iterate each character in the first string, checking if they are contained within the second string. This is fairly inefficient because it iterates each character of the original string rather than the string as it's being changed. So if the first string contains duplicate characters itself, it will iterate them but not do anything after the first pass.

Dim string1 As String = "horse"
Dim string2 As String = "donkey"

For Each c As Char In string1
    If string2.Contains(c) Then
        string1 = string1.Replace(c.ToString(), "")
        string2 = string2.Replace(c.ToString(), "")
    End If
Next

Another option is to use LINQ to find the intersection of the two strings. This would give you a distinct list of the common characters between the two strings.

Dim string1 As String = "horse"
Dim string2 As String = "donkey"

Dim ar() As Char = string1.Intersect(string2).ToArray()

For Each c As Char In ar
    string1 = string1.Replace(c.ToString(), "")
    string2 = string2.Replace(c.ToString(), "")
Next

Edit: In case you have strings that contain upper and lower case letters, you need to handle that manually since Replace() doesn't.

You can check out this answer for a couple different ways to extend the capabilities of Replace(). Or, if you aren't too worried about performance, just use ToUpper() and ToLower().

For Each c As Char In string1
    If string2.Contains(c.ToString().ToLower()) Or string2.Contains(c.ToString().ToUpper()) Then
        string1 = string1.Replace(c.ToString().ToLower(), "")
        string2 = string2.Replace(c.ToString().ToLower(), "")

        string1 = string1.Replace(c.ToString().ToUpper(), "")
        string2 = string2.Replace(c.ToString().ToUpper(), "")
    End If
Next
Community
  • 1
  • 1
j.f.
  • 3,908
  • 2
  • 29
  • 42
  • Just incase you read this can you help me one more time if i enter capital letters and the letter that is common to that capital one is in lowercase it's not going to cancel it I tried making the said letter to lower case with this code but it doesn't look like it is working If strname.IndexOf(0).ToString.ToUpper Then TextBox1.Text.ToLower() End If If strname2.IndexOf(0).ToString.ToUpper Then textbox2.text.ToLower() End If – anitstudent Oct 16 '14 at 16:51
  • So if there is an "F" in string1 and an "f" in string2, you do or don't want to remove them? – j.f. Oct 16 '14 at 17:33
  • I want to remove it since in the rules of flames you need to do so – anitstudent Oct 16 '14 at 17:59
  • Ok. I'm not familiar with flames. See my edit above. – j.f. Oct 16 '14 at 18:14