1

I'm trying to remove all non-ascii characters from an input string using a regular expression in VB.NET (3.5). I have a function that should run any input string through a regular expression:

Public Shared Function RemoveIllegalCharacters(ByVal inpTxt As String) As String

    'use a regular expression to replace any characters that are non-ascii
    inpTxt = Regex.Replace(inpTxt, "[^\u0000-\u007F]", String.Empty)

    Return inpTxt

End Function

Which seems to work properly within the Function. inpTxt = "123foobar" is "123foobar" throughout the function. However, when I access it elsewhere:

Public someOtherFunction(ByVal inpTxt As String) As String

    inpTxt = RemoveIllegalCharacters(inpTxt)

    Return inpTxt

End Function

the first character disappears:

inpTxt = "23foobar"

Other sources suggests I write

inpTxt = Regex.Replace(inpTxt, @"[^\u0000-\u007F]", String.Empty)

But the project refuses to compile without a string for the second parameter of Regex.Replace.

4
  • @"[^\u0000-\u007F]" is a verbatim string literal in C#. You can't use @"" in VB (nor would you need to).
    – JDB
    Jun 14, 2013 at 17:23
  • 3
    Your Sub doesn't make sense. You are passing inpTxt ByVal, assigning it, and leaving the function. That doesn't do anything.
    – vcsjones
    Jun 14, 2013 at 17:24
  • Other code bits were removed. Jun 14, 2013 at 17:31
  • 1
    Open a brand new console project and write something that simply reproduces the problem. If you can reproduce it, update your question with the complete code necessary to reproduce your results. My guess is you've got something else in your code which is the source of this issue.
    – JDB
    Jun 14, 2013 at 17:58

1 Answer 1

2

This function is meaningless:

Public Shared Sub RemoveIllegalCharacters(ByVal inpTxt As String)

    'use a regular expression to replace any characters that are non-ascii
    inpTxt = Regex.Replace(inpTxt, "[^\u0000-\u007F]", String.Empty)

End Sub

If inpTxt is passed ByVal, then this function does not do anything. It doesn't change the string from the caller, the assignment is only valid inside of the Sub. You can change your Sub to a Function and return it instead:

Public Shared Function RemoveIllegalCharacters(ByVal inpTxt As String) As String

    'use a regular expression to replace any characters that are non-ascii
    Return Regex.Replace(inpTxt, "[^\u0000-\u007F]", String.Empty)

End Function

And use it like so:

Dim cleaned = RemoveIllegalCharacters(inpTxt)

This appears to work:

Dim inpTxt = "1234FOOBARR" + Chr(&H80)
Console.WriteLine(inpTxt) 'Prints "1234FOOBARR?"
Dim cleaned = RemoveIllegalCharacters(inpTxt)
Console.WriteLine(cleaned) 'Prints "1234FOOBARR"
2
  • vcsjones, I changed my sub to a function (see in edits above), but when I debug through the code I still see the return text losing the first character. Jun 14, 2013 at 17:48
  • 2
    @user416195 Then there is something else in play here that we are not seeing. Putting this code into a brand-new Console project does not have that behavior.
    – vcsjones
    Jun 14, 2013 at 17:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.