473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

checking string for all blanks


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
--
Posted via a free Usenet account from http://www.teranews.com

May 10 '07 #1
6 1421
If MyString.Trim = "" Then
....
End If

Cheers,
Johnny J.


"Jeff" <no**@nothingX.comwrote in message
news:46**********************@free.teranews.com...
>
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
--
Posted via a free Usenet account from http://www.teranews.com

May 10 '07 #2
Jeff wrote:
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
May 10 '07 #3
Jeff wrote:
>
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
_____
http://www.guffa.com
May 10 '07 #4

"Göran Andersson" <gu***@guffa.comwrote in message
news:uJ**************@TK2MSFTNGP02.phx.gbl...
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

--
Posted via a free Usenet account from http://www.teranews.com

May 11 '07 #5
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" <no**@nothingX.comwrote in message
news:46***********************@free.teranews.com.. .
>
"Göran Andersson" <gu***@guffa.comwrote in message
news:uJ**************@TK2MSFTNGP02.phx.gbl...
>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

--
Posted via a free Usenet account from http://www.teranews.com
May 11 '07 #6
Stephany Young wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
May 14 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
19
by: linzhenhua1205 | last post by:
I want to parse a string like C program parse the command line into argc & argv. I hope don't use the array the allocate a fix memory first, and don't use the memory allocate function like malloc....
9
by: Jacob | last post by:
Is there any way (maybe using an attribute) that I can force strings or other reference parameters to always have an instance even if they are blank? Ex: public string DoSomething(string Text)...
13
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I...
3
by: Rich Shepard | last post by:
I need to learn how to process a byte stream from a form reader where each pair of bytes has meaning according to lookup dictionaries, then use the values to build an array of rows inserted into a...
8
by: Count Dracula | last post by:
The following three functions check whether a given string represents a valid number. They require that the entire string represent a number, unlike strtod which accepts strings like "99rQF" and...
42
by: =?Utf-8?B?UGxheWE=?= | last post by:
I have an if statement that isn't working correctly and I was wondering how I check for a blank string. My Code Example if me.fieldname(arrayIndex) = "" then ----- end if When I do this and...
10
by: Diego F. | last post by:
Hi all. I have an application that receives a message from a socket in an array from a certain size. As the array size may be longer that the message received, the end of the array has blank...
27
by: Aaron Hsu | last post by:
Hey all, After seeing the Secure version I/O functions thread, it occured to me that maybe not everyone agrees with the almost universal adage that I have heard. I have Always been told that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.