Remove invalid character from string

Hello -- this is probably super easy so forgive my lack of knowledge here ... but can someone help me out with this:

I have a VB program that parses a text file. Within that text file, however, are "unrecognized characters" that show up as blocks when the file is opened in Notepad. Ex -- DCCG00000

When I actually parse some text from one of these files that includes this character and output it to the Console it appears as a double arrow similar to this: DCCG00000<->

How do I programmatically identify and remove these characters

TIA!



Answer this question

Remove invalid character from string

  • bkustel

    If you just wanted to remove non-printable characters, you could create a function:

    Public Function CleanString(ByVal Input As String) As String
    Dim poTemp As New System.Text.StringBuilder
    Dim piIndex As Integer

    For piIndex = 0 To Input.Length - 1
    If Input.Substring(piIndex, 1) >= " " And Input.Substring(piIndex, 1) <= "z" Then
    poTemp.Append(Input.Substring(piIndex, 1))
    End If
    Next

    CleanString = poTemp.ToString
    End Function

    You can change the " " and "z" in the IF to whatever ASCII values you desire in order to keep just the characters you wanted.


  • Cwilson

    Thanks everyone!


  • Learning VB

    Here is a good example to do that :
    Dim Stn As String '= Something
    Dim VaildCharacters As String = "abcdefghijklmnopqrstuvwxyz" 'The vaild (acceptable) characters
    Dim TempIndex As Integer
    For TempIndex = 0 To Stn.Length - 1
    If VaildCharacters.LastIndexOf(Stn.Substring(TempIndex, 1).ToLower) > -1 Then TextBox1.Text &= Stn.Substring(TempIndex, 1)
    Next

    Hope this helps you a bit.


  • VOC

    Besides having the following character methods to test your characters:

    Char.IsLetterOrDigit()

    Char.IsPunctuation()

    Char.IsSymbol()

    You can create a function that fits your needs such as:

    Public Function IsValidCharacter(byval c as Char)as boolean 

    if AscW(c) < 40 or AscW(c) > 176 Then

    Return False

    Else

    Return True

    End If

    End Function



  • Remove invalid character from string