locked
how to remove numbers from a string RRS feed

  • Question

  • Hi there
    Could anyone tell the answer to this question:
    How can you remove number from a sting. The numbers could be anywhere in the sting and they could be any combination of numbers but would be together in the string. Ie abcdefg321hijk....

    Any ideas ?
    Cheers.
    Paul

    Sunday, November 29, 2009 7:08 PM

Answers

  • Use this!

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = TextBox1.Text.Replace("1", "")
            TextBox1.Text = TextBox1.Text.Replace("2", "")
            TextBox1.Text = TextBox1.Text.Replace("3", "")
            TextBox1.Text = TextBox1.Text.Replace("4", "")
            TextBox1.Text = TextBox1.Text.Replace("5", "")
            TextBox1.Text = TextBox1.Text.Replace("6", "")
            TextBox1.Text = TextBox1.Text.Replace("7", "")
            TextBox1.Text = TextBox1.Text.Replace("8", "")
            TextBox1.Text = TextBox1.Text.Replace("9", "")
            TextBox1.Text = TextBox1.Text.Replace("0", "")
        End Sub

    www.shariqdon.media.officelive.com
    • Edited by Shariq Ayaz Sunday, November 29, 2009 7:47 PM
    • Marked as answer by YiChun Chen Tuesday, December 1, 2009 9:14 AM
    Sunday, November 29, 2009 7:34 PM
  • Seems like a good place for a regex:

      Function RemoveDigits(ByVal S As String) As String
        Return RegularExpressions.Regex.Replace(S, "\d", "")
      End Function
    • Proposed as answer by Crazypennie Sunday, November 29, 2009 9:13 PM
    • Marked as answer by YiChun Chen Tuesday, December 1, 2009 9:14 AM
    Sunday, November 29, 2009 8:34 PM
  • or you can loop through all the characters, check if not a number and append it to a string using StringBuilder


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myString As String = "abc1def2ghi34"
            Dim sb As New StringBuilder
    
            For Each c As Char In myString
                If Not Char.IsNumber(c) Then
                    sb.Append(c)
                End If
            Next
            Debug.WriteLine(sb.ToString)
    
        End Sub
    • Proposed as answer by Crazypennie Sunday, November 29, 2009 9:14 PM
    • Marked as answer by YiChun Chen Tuesday, December 1, 2009 9:14 AM
    Sunday, November 29, 2009 8:15 PM

All replies

  • Use this!

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = TextBox1.Text.Replace("1", "")
            TextBox1.Text = TextBox1.Text.Replace("2", "")
            TextBox1.Text = TextBox1.Text.Replace("3", "")
            TextBox1.Text = TextBox1.Text.Replace("4", "")
            TextBox1.Text = TextBox1.Text.Replace("5", "")
            TextBox1.Text = TextBox1.Text.Replace("6", "")
            TextBox1.Text = TextBox1.Text.Replace("7", "")
            TextBox1.Text = TextBox1.Text.Replace("8", "")
            TextBox1.Text = TextBox1.Text.Replace("9", "")
            TextBox1.Text = TextBox1.Text.Replace("0", "")
        End Sub

    www.shariqdon.media.officelive.com
    • Edited by Shariq Ayaz Sunday, November 29, 2009 7:47 PM
    • Marked as answer by YiChun Chen Tuesday, December 1, 2009 9:14 AM
    Sunday, November 29, 2009 7:34 PM
  • I feel silly now !
    Cheers mate.
    Sunday, November 29, 2009 7:43 PM
  • or you can loop through all the characters, check if not a number and append it to a string using StringBuilder


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myString As String = "abc1def2ghi34"
            Dim sb As New StringBuilder
    
            For Each c As Char In myString
                If Not Char.IsNumber(c) Then
                    sb.Append(c)
                End If
            Next
            Debug.WriteLine(sb.ToString)
    
        End Sub
    • Proposed as answer by Crazypennie Sunday, November 29, 2009 9:14 PM
    • Marked as answer by YiChun Chen Tuesday, December 1, 2009 9:14 AM
    Sunday, November 29, 2009 8:15 PM
  • Seems like a good place for a regex:

      Function RemoveDigits(ByVal S As String) As String
        Return RegularExpressions.Regex.Replace(S, "\d", "")
      End Function
    • Proposed as answer by Crazypennie Sunday, November 29, 2009 9:13 PM
    • Marked as answer by YiChun Chen Tuesday, December 1, 2009 9:14 AM
    Sunday, November 29, 2009 8:34 PM