2 Replies - 1115 Views - Last Post: 11 November 2012 - 05:41 PM Rate Topic: -----

#1 ninjAl420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-November 12

Visual Studio, Replacing instances of bad.substring with good.substrin

Posted 11 November 2012 - 02:18 PM

Im using Visual Basic 2010. Im having the user enter a string, good substring, and bad substring (Three text Boxes). I need to have my code find the instances of the bad substring in my string, and replaces each instance with the good substring. I feel like there should be a loop to determine the instances of the bad substring. I just can't wrap my mind around it. Here is all I have so far. Hopefully someone can help me.

    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click

        Dim strEnteredString As String = txtInputString.Text.Trim
        Dim intEnteredStringChars As Integer = strEnteredString.Length
        Dim strGoodSubstring As String = txtInputGoodString.Text.Trim
        Dim intGoodSubstringChars As Integer = strGoodSubstring.Length
        Dim strBadSubstring As String = txtInputBadString.Text.Trim
        Dim intBadSubstringChars As Integer = strBadSubstring.Length


        Dim strNewString As String = strEnteredString
        Dim strNewStringFinal As String


        If strEnteredString.Contains(strBadSubstring) Then
            strNewStringFinal = strNewString.Remove(
            strNewString = strEnteredString.Insert(intBadSubstringChars, strGoodSubstring)


        End If


Is This A Good Question/Topic? 0
  • +

Replies To: Visual Studio, Replacing instances of bad.substring with good.substrin

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

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

Re: Visual Studio, Replacing instances of bad.substring with good.substrin

Posted 11 November 2012 - 05:23 PM

You do realize that the String class has a Replace() method right?

Dim testStr as String = "the brown fox jumped over the lazy dog"

Dim newStr as String = testStr.Replace("the", "a")

' Prints out "a brown fox jumped over a lazy dog"
Console.WriteLine(newStr)



Unless you are doing it the long way for some reason. :)

This post has been edited by Martyr2: 11 November 2012 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 ninjAl420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-November 12

Re: Visual Studio, Replacing instances of bad.substring with good.substrin

Posted 11 November 2012 - 05:41 PM

As a matter of fact I am trying to do it the long way haha. I had the if statement but cant figure out how to idwntify remove and replace without using. Replace. Thank you for taking the time to help me toooo :-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1