checking string for all blanks

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Could someone tell me the easiest way to check a string of variable length
to see if it consists of all blank characters?

....or perhaps more generally, to see if all of the characters are the same.


Thanks

Jeff
 
Jeff said:
Could someone tell me the easiest way to check a string of variable
length to see if it consists of all blank characters?

Assuming an empty string counts as all blank chars,

If Len(Trim(someString))=0 then...
...or perhaps more generally, to see if all of the characters are the
same.

If Len(someString)>0 AndAlso Len(someString.Trim(someString.Chars(1))) = 0
Then...

Andrew
 
Jeff said:
Could someone tell me the easiest way to check a string of variable
length to see if it consists of all blank characters?

...or perhaps more generally, to see if all of the characters are the same.

The most resource effecive and scalable would be to just loop the string:

Function IsAllSameCharacter(s As String)
Dim first As Char = s.Chars(0)
ForEach c As Char in s
If c <> first Return False
Next
Return True
End Function

[Disclaimer: Code is not tested]
 
Göran Andersson said:
Jeff wrote:
The most resource effecive and scalable would be to just loop the string:

Function IsAllSameCharacter(s As String)
Dim first As Char = s.Chars(0)
ForEach c As Char in s
If c <> first Return False
Next
Return True
End Function

[Disclaimer: Code is not tested]

Other than a typo involving a missing space and one other, it ran fine.
Thanks - my tested code is below. I don't know enough to determine whether
this is the most resource effective relative to the other alternatives
provided, but I will take your word for it. Jeff

Function IsAllSameCharacter(ByVal s As String)
Dim first As Char = s.Chars(0)
For Each c As Char In s
If c <> first Then
Return False
End If
Next
Return True
End Function
 
Take his word for it that it is certainly resource effective and scalable.

Whether it is the most ... is a matter of debate.

For short strings it will be lightening fast.

For longer strings, the further it has to loop through the characters the
longer it will take and you might find it quicker to do something like:

Function IsAllSameCharacter(ByVal s As String)

Dim _s As String = New String(s.Chars(0), s.Length)

Return (s = _s)

End Function



Jeff said:
Göran Andersson said:
Jeff wrote:
The most resource effecive and scalable would be to just loop the string:

Function IsAllSameCharacter(s As String)
Dim first As Char = s.Chars(0)
ForEach c As Char in s
If c <> first Return False
Next
Return True
End Function

[Disclaimer: Code is not tested]

Other than a typo involving a missing space and one other, it ran fine.
Thanks - my tested code is below. I don't know enough to determine whether
this is the most resource effective relative to the other alternatives
provided, but I will take your word for it. Jeff

Function IsAllSameCharacter(ByVal s As String)
Dim first As Char = s.Chars(0)
For Each c As Char In s
If c <> first Then
Return False
End If
Next
Return True
End Function
 
Stephany said:
Take his word for it that it is certainly resource effective and scalable.

Whether it is the most ... is a matter of debate.

For short strings it will be lightening fast.

Yes, but that is not very interresting. For short strings it doesn't
really matter. It's what happens when the strings get larger that
matters for scalability.
For longer strings, the further it has to loop through the characters
the longer it will take and you might find it quicker to do something like:

Function IsAllSameCharacter(ByVal s As String)

Dim _s As String = New String(s.Chars(0), s.Length)

Return (s = _s)

End Function

But that will make two loops instead of one. First it will do a loop to
initialise the new string, then it will do another loop to compare all
the characters between the strings. At best (when all characters are the
same) it's half as fast, at worst (when the first two characters differ)
it's much slower, as the first loop will always loop through all the
characters regardless if they are used or not.

As this method creates another string that is as large as the original
string, it does not scale well. The method that I presented only
allocates memory for a repeater, and that is the same regardless of the
size of the string.
 
Back
Top