Need code for removing all unicode characters in vb6

2k views Asked by At

I need code for removing all unicode characters in a vb6 string.

3

There are 3 answers

0
Bob77 On BEST ANSWER

If this is UTF-16 text (as normal VB6 String values all are) and you can ignore the issue of surrogate pairs, then this is fairly quick and reasonably concise:

Private Sub DeleteNonAscii(ByRef Text As String)
    Dim I As Long
    Dim J As Long
    Dim Char As String

    I = 1
    For J = 1 To Len(Text)
        Char = Mid$(Text, J, 1)
        If (AscW(Char) And &HFFFF&) <= &H7F& Then
            Mid$(Text, I, 1) = Char
            I = I + 1
        End If
    Next
    Text = Left$(Text, I - 1)
End Sub

This has the workaround for the unfortunate choice VB6 had to make in returning a signed 16-bit integer from the AscW() function. It should have been a Long for symmatry with ChrW$() but it is what it is.

It should beat the pants off any regular expression library in clarity, maintainability, and performance. If better performance is required for truly massive amounts of text then SAFEARRAY or CopyMemory stunts could be used.

0
AudioBubble On

StrConv is the command for converting strings.

StrConv Function

Returns a Variant (String) converted as specified.

Syntax

StrConv(string, conversion, LCID)

The StrConv function syntax has these named arguments:

Part Description

string Required. String expression to be converted. 

conversion Required. Integer. The sum of values specifying the type of conversion to perform. `128` is Unicode to local code page (or whatever the optional LCID is) 

LCID Optional. The LocaleID, if different than the system LocaleID. (The system LocaleID is the default.) 
4
Shane On
Public Shared Function StripUnicodeCharactersFromString(ByVal inputValue As String) As String
    Return Regex.Replace(inputValue, "[^\u0000-\u007F]", String.Empty)
End Function

Vb6 - not sure will

sRTF = "\u" & CStr(AscW(char))

work? - You could do this for all char values above 127