Consulting

Results 1 to 5 of 5

Thread: How to check if string includes any non-whitespace characters?

  1. #1
    VBAX Regular
    Joined
    Mar 2021
    Posts
    8
    Location

    How to check if string includes any non-whitespace characters?

    Is there a simple way to check if a string includes non-whitespace characters? I don't need to know what the character is, just that the string includes a non-whitespace character. For example, I want to know if the string includes any characters other than:

    vbCr Chr(13) Carriage return character
    vbLf Chr(10) Linefeed character
    vbCrLf Chr(13) + Chr(10) Carriage return - linefeed combination
    vbNewLine Chr(13) + Chr(10) New line character
    vbTab Chr(9) Tab character
    vbBack Chr(8) Backspace character
    Last edited by SFRandy; 04-13-2021 at 12:26 PM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Posts
    7,723
    Location
    One way


    Option Explicit
    
    
    Sub test()
        Dim s1 As String, s2 As String
        
        s1 = "asdf" & vbCrLf & "asdf"
        s2 = vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf
    
    
        If ContainsReqularChars(s1) Then
            MsgBox "Some regular chars"
        Else
            MsgBox "Nothing but non-regular chars"
        End If
    
    
        If ContainsReqularChars(s2) Then
            MsgBox "Some regular chars"
        Else
            MsgBox "Nothing but non-regular chars"
        End If
    End Sub
    
    
    Private Function ContainsReqularChars(s As String) As Boolean
        Dim s1 As String
        s1 = Replace(s, vbCr, vbNullString)
        s1 = Replace(s1, vbLf, vbNullString)
        s1 = Replace(s1, vbTab, vbNullString)
        s1 = Replace(s1, vbBack, vbNullString)
        
        ContainsReqularChars = (Len(s1) > 0)
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Mar 2021
    Posts
    8
    Location
    Quote Originally Posted by Paul_Hossler View Post
    One way
    Thank you! This is exactly the type of clever solution I was hoping for!

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,534
    Location
    Yet Another way:
    Private Function ContainsReqularChars(selected As String) As Boolean
    Dim Char As Range
        Const WhiteSpaces As String = vbCr & vbLf & vbNewLine & vbTab & vbBack & " " 
    
    For each Char in selected
       If Not Instr(Whitespaces, Char) Then 
          'If we find one Regular, then we're done
          ContainsReqularChars = True
          Exit Function
       End If
    Next Char
    End Function
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Posts
    7,723
    Location
    Yet Another Another way -

    If time is really, really critical messing with Strings takes longer than numbers

    If VBA had an intrinsic Min function, it'd be even easier and faster

    Option Explicit
    
    
    Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    
    
    Sub test()
        Dim s1 As String, s2 As String, s3 As String
        
        s1 = "asdf" & vbCrLf & "asdf"
        s2 = vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf
        s3 = "asfasfasfasdfasdf"
    
    
        If OnlyRegularChars(s1) Then
            MsgBox "All regular chars"
        Else
            MsgBox "Some non-Regular chars"
        End If
    
    
    
    
        If OnlyRegularChars(s2) Then
            MsgBox "All regular chars"
        Else
            MsgBox "Some non-Regular chars"
        End If
    
    
        If OnlyRegularChars(s3) Then
            MsgBox "All regular chars"
        Else
            MsgBox "Some non-Regular chars"
        End If
    End Sub
    
    
    
    
    Function OnlyRegularChars(s As String) As Boolean
        Dim aStringAsInteger() As Integer
        Dim i As Long
        
        OnlyRegularChars = False
        
        'copy string into integer array
        ReDim aStringAsInteger((LenB(s) - 1) \ 2)
        CopyMemory aStringAsInteger(0), ByVal StrPtr(s), LenB(s)
                
        'have to check the Integer array
        For i = LBound(aStringAsInteger) To UBound(aStringAsInteger)
            If aStringAsInteger(i) < 32 Then Exit Function
        Next i
        
        OnlyRegularChars = True
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •