1

This is more like a logical question, I just need an idea on how to best approach this problem.

I am trying to do this in visual basic. I have to text box, in text box 1, I would have some text and if it contains certain word, I want to replace that with something else and have the new output in text box 2, after clicking on a convert button.

I know this can be done really easily with the replace function built in, but I am trying to do it without the replace function.

For example:

I want to replace "fox" with "dog"

Textbox 1: The quick brown fox jumped over the lazy dog.

Textbox 2: The quick brown dog jumped over the lazy dog.

I got it to work with the code below, but this only works for the first instance. How can I change my code for all instances.

String from InputText
    Dim S1 As String
    S1 = InputText.Text

    'Position of start of "Edge Computer"
    Dim pStart As Integer
    pStart = S1.IndexOf("Edge Computer")

    'Position of end of "Edge Computer"
    Dim pEnd As Integer
    pEnd = S1.IndexOf("r", pStart) + 1

    'Actual old name as string
    Dim strOld As String
    strOld = S1.Substring(pStart, 13)

    'New String for Output
    Dim S2 As String
    S2 = S1.Substring(0, pStart) & "3dg3" &      S1.Substring(pEnd)

    OutputText.Text = S2

Thanks

2
  • Is it just the old global Replace keyword from VB6 you want to avoid? Would you be ok with the String.Replace or Regex.Replace functions that are fully ".net compliant"? Commented Apr 9, 2015 at 17:15
  • I can't use any replace function. I am trying to do it using count or loop or whatever. Commented Apr 9, 2015 at 17:42

1 Answer 1

1

If you don't want to use replace you could use substring to select the text before and after the word that as to be replaced.

You did not post any code so it's hard to put an example that would fit you better

Dim NewText as String 
If textbox1.text.IndexOf("OldWord") > - 1 Then
    NewText = textbox1.text.Substring(0, textbox1.text.IndexOf("OldWord")) & "NewWord " & textbox1.text.Substring(textbox1.text.IndexOf("OldWord") + "Something".length, textbox1.text.length  ) 
   'Make sure you replacfe OldWold and NewWord to the specific text or variable 
End If

To make it work with all instance of the word, you just need to replace the if by a loop

Dim NewText as String
Dim OldText as String = textbox1.text

While OldText.IndexOf("OldWord") > - 1
    NewText = OldText.Substring(0, OldText.IndexOf("OldWord")) & "NewWord " & OldText.Substring(OldText.IndexOf("OldWord") + "OldWord".length, OldText.length  ) 
    OldText = OldText.Substring(OldText.IndexOf("OldWord") + "OldWord".length, OldText.length)
   'Make sure you replace OldWold and NewWord to the specific text or variable 

End While

Let me know if this works or not

Sign up to request clarification or add additional context in comments.

3 Comments

I got it to work with this code above, but it only works for the first instance of the word, I want to change my code for all instances of the word.
@Agent_Sully I updated my answer to make it work with all instance
@Agent_Sully If this answer works for you, don't forget to click the green check mark to mark this question as answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.