Find and Replace in Textbox

Nessie

Member
Joined
Oct 14, 2011
Messages
20
Programming Experience
1-3
Hi All,

Just a small plea for help :)

I have a textbox which I copy data into and then save it to a text file but I have multiple occurrences where words are repeated twice and it's these that I want to replace e.g.

Email Address:
Employee Number:
Department:
.
.
.
Email Address:
Employee Number:
Department:

I would like these to be is:

Email Address1:
Employee Number1:
Department1:
.
.
.
Email Address2:
Employee Number2:
Department2:

I have tried doing this in a loop using

For i = 1 to 2

textbox1.text.replace("Email Address:", "Email Address" + i ":")

Next i

but failed, any help would be greatly appreciated.

Regards
 
This is in the wrong section btw ;) Next time put it in the WinForms section

 Dim StringToReplace As String = "Email Address:"
Dim ReplaceWith As String = ""
Dim S1 As String = ""
Dim S2 As String = ""


If TextBox1.Text.Contains(StringToReplace) Then
    Dim aEmails As Integer = Regex.Matches(TextBox1.Text, Regex.Escape(StringToReplace)).Count
    For i = 1 To aEmails
        Dim myText As String = TextBox1.Text
        TextBox1.Text = ""
        ReplaceWith = "Email Address" + i.ToString + ":"
        S1 = myText.Substring(0, myText.IndexOf(StringToReplace))
        S2 = myText.Substring(myText.IndexOf(StringToReplace) + StringToReplace.Length(), myText.Length - (myText.IndexOf(StringToReplace) + StringToReplace.Length()))
        TextBox1.Text += S1 & ReplaceWith & S2
    Next
End If


This goes through the text box and finds all the email addresses, then replaces the Email Address string and adds a number after it.
I didn't think it would be this complicated and I think there might be a simpler way of doing it :p

Regards,
Bryce Gough
 
Last edited:
How are you copying the data and what's the source? This would be far easier to achieve as part of the copying process.
 
VB.NET:
textbox1.text.replace("Email Address:", "Email Address" + i ":")

should be


VB.NET:
textbox1.text = textbox1.text.replace("Email Address:", "Email Address" + i ":")
 
VB.NET:
textbox1.text.replace("Email Address:", "Email Address" + i ":")

should be


VB.NET:
textbox1.text = textbox1.text.replace("Email Address:", "Email Address" + i ":")

Well actually,

VB.NET:
textbox1.text = textbox1.text.replace("Email Address:", "Email Address" [B]&[/B] i [B]&[/B] ":")

or, if Option Strict,

VB.NET:
textbox1.text = textbox1.text.replace("Email Address:", "Email Address" [B]&[/B] i.ToString [B]&[/B] ":")
 
Back
Top