View Full Version : How to check if string includes any non-whitespace characters?
SFRandy
04-13-2021, 12:13 PM
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
Paul_Hossler
04-13-2021, 01:01 PM
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
SFRandy
04-14-2021, 09:01 AM
One way
Thank you! This is exactly the type of clever solution I was hoping for!
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
Paul_Hossler
04-20-2021, 12:54 PM
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
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.