Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB6,VBS
Expertise: Intermediate
Feb 26, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

StripControlChars - Delete control characters in a string

' Strip all control characters (ASCII code < 32)
' If the second argument is True or omitted,
' CR-LF pairs are preserved

Function StripControlChars(source As String, Optional KeepCRLF As Boolean = _
    True) As String
    Dim index As Long
    Dim bytes() As Byte
    ' the fastest way to process this string
    ' is copy it into an array of Bytes
    bytes() = source
    For index = 0 To UBound(bytes) Step 2
        ' if this is a control character
        If bytes(index) < 32 And bytes(index + 1) = 0 Then
            If Not KeepCRLF Or (bytes(index) <> 13 And bytes(index) <> 10) Then
                ' the user asked to trim CRLF or this
                ' character isn't a CR or a LF, so clear it
                bytes(index) = 0
            End If
        End If
    ' return this string, after filtering out all null chars
    StripControlChars = Replace(bytes(), vbNullChar, "")
End Function
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date