replacment of characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gyap88
    New Member
    • Jul 2007
    • 37

    #1

    replacment of characters

    How do I replace the one of the characters in the string.
    For eg:

    If the users were to input a few characters into textbox1, i will replace the 1st character of the string with the character "A" and return the edited string to display in textbox2.
  • brunzire
    New Member
    • Aug 2007
    • 9

    #2
    use the instr function to determine the postion of the string

    eg retvalue = instr(string to search, "what to search for")

    This will tell you where the string is char you want to remove is

    see example below i have used before in vb6 (may need a tweek in dot net)

    *************Co de goes here*********** *************** ************

    Function RemoveChar(Name As String, Char As String)
    'This function removes a Character from a string variable
    ' The character must be specified when calling the function
    ' eg call removechar(stri ng1,",") removes all commas from a string
    Dim x
    Dim FIRSTPART
    Dim SECONDPART

    If InStr(1, Name, Char) Then
    x = InStr(1, Name, Char)
    FIRSTPART = Left(Name, x - 1)
    SECONDPART = Mid(Name, x + 1, Len(Name))
    Name = FIRSTPART + SECONDPART
    End If

    Do While InStr(1, Name, Char) > 0
    x = InStr(1, Name, Char)
    FIRSTPART = Left(Name, x - 1)
    SECONDPART = Mid(Name, x + 1, Len(Name))
    Name = FIRSTPART + SECONDPART
    Loop

    RemoveChar = Name
    End Function

    *********End Code goes here *************** *************** ****

    Comment

    • JonJacobs
      New Member
      • Aug 2007
      • 22

      #3
      Let's see if I understand your question:

      User types 12345 in textbox1. You want to put A2345 into textbox2.

      If I understood correctly, try this code (vb.net):

      Dim S as string = TextBox1.Text
      TextBox2.Text = "A" + S.Substring(1)

      HTH

      Jon Jacobs

      Comment

      • edpudol
        New Member
        • Aug 2007
        • 5

        #4
        if JonJacobs understanding is correct you may also want to try this for VB 6

        retVal = "A" & right(Text1.Tex t,len(Text1.Tex t) -1)


        But if you're trying to search for a word from a group of words then Instr is good solution...

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by edpudol
          retVal = "A" & right(Text1.Tex t,len(Text1.Tex t) -1)
          You can actually make it even simpler. Try this...

          retVal = "A" & Mid(Text1.Text, 2)

          Comment

          • brunzire
            New Member
            • Aug 2007
            • 9

            #6
            You should use functions for repeatable code as much as possible. The function i have given you allows you to pass in whatever character replacement that you want.

            This reduces the amount of code that you need to write hence reducing the size of your app (performance)

            Comment

            • brunzire
              New Member
              • Aug 2007
              • 9

              #7
              i have modified my original code to handle replacement, rather than remove

              it will need to be tested
              It will be called as

              Dim StringIwanttodi splay as string

              Stringiwanttodi splay = ChangeChar(Stri ngiwanttodispla y,1,2)

              The will replace all 1s with 2s in your string



              Function ChangeChar(Name As String, Char_to_Change As String, Char_to_Change_ to as String )

              Dim x
              Dim FIRSTPART
              Dim SECONDPART

              If InStr(1, Name, Char) Then
              x = InStr(1, Name, Char)
              FIRSTPART = Left(Name, x - 1)
              SECONDPART = Mid(Name, x + 1, Len(Name))
              Name = FIRSTPART + Char_to_Change_ to + SECONDPART
              End If

              Do While InStr(1, Name, Char) > 0
              x = InStr(1, Name, Char)
              FIRSTPART = Left(Name, x - 1)
              SECONDPART = Mid(Name, x + 1, Len(Name))
              Name = FIRSTPART + Char_to_Change_ to + SECONDPART
              Loop

              RemoveChar = Name
              End Function

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by brunzire
                You should use functions for repeatable code as much as possible ... hence reducing the size of your app (performance)
                Good advice there, brunzire.

                Comment

                Working...