473,686 Members | 3,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Empty string comparisons

Hi all,

I am having a memory blank at the moment. I have been writing in C# for a
number of years and now need to do something in VB.NET, so forgive me such a
primitive question.

In C#, I test whether a string has a value or not by the following syntax:

if (thisString.Tri m() == "")
{
// if true
something()
}
else
{
// if false
somethingelse()
}

Now, in VB.NET an equivalent does not seem to work:

If thisString.Trim () = "" Then
' if true
something()
Else
' if false
somethingelse()
End If

I cannot get past a False return for the VB comparison. What is the best
method for testing whether a string is empty or null?
Regards,
Neville Lang
Oct 25 '06 #1
26 3800
Hi Neville

Don't know whether this is 'best' or not but it certainly works:

If testStr Is Nothing or Len(Trim(testSt r)) = 0 Then
' 0 length or null string
Else
' string is initialised and not empty
End if

Cheers
Martin

Neville Lang wrote:
Hi all,

I am having a memory blank at the moment. I have been writing in C# for a
number of years and now need to do something in VB.NET, so forgive me such a
primitive question.

In C#, I test whether a string has a value or not by the following syntax:

if (thisString.Tri m() == "")
{
// if true
something()
}
else
{
// if false
somethingelse()
}

Now, in VB.NET an equivalent does not seem to work:

If thisString.Trim () = "" Then
' if true
something()
Else
' if false
somethingelse()
End If

I cannot get past a False return for the VB comparison. What is the best
method for testing whether a string is empty or null?
Regards,
Neville Lang
Oct 25 '06 #2

Neville Lang ha scritto:

Now, in VB.NET an equivalent does not seem to work:

If thisString.Trim () = "" Then
' if true
something()
Else
' if false
somethingelse()
End If

I usually use:

Dim s As String = ""
If s.Trim = String.Empty Then
MsgBox("is empty")
Else
MsgBox("is not empty")
End If

but your version also looks fine to me (unless I am missing to see
something)...
Tommaso
>
I cannot get past a False return for the VB comparison. What is the best
method for testing whether a string is empty or null?
Regards,
Neville Lang
Oct 25 '06 #3
Your problem probably lies elsewhere - the VB code you posted is the exact
equivalent to the C# code.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"Neville Lang" wrote:
Hi all,

I am having a memory blank at the moment. I have been writing in C# for a
number of years and now need to do something in VB.NET, so forgive me such a
primitive question.

In C#, I test whether a string has a value or not by the following syntax:

if (thisString.Tri m() == "")
{
// if true
something()
}
else
{
// if false
somethingelse()
}

Now, in VB.NET an equivalent does not seem to work:

If thisString.Trim () = "" Then
' if true
something()
Else
' if false
somethingelse()
End If

I cannot get past a False return for the VB comparison. What is the best
method for testing whether a string is empty or null?
Regards,
Neville Lang
Oct 25 '06 #4
Pritcham wrote:
Hi Neville

Don't know whether this is 'best' or not but it certainly works:

If testStr Is Nothing or Len(Trim(testSt r)) = 0 Then
' 0 length or null string
Else
' string is initialised and not empty
End if
Just in case you're not aware, the Len() and Trim() functions understand
strings that are Nothing and treat them as empty strings (unlike
testStr.Length( ) or testStr.Trim(), which would obviously throw a
NullReferenceEx ception), so you can actually shorten this to:

\\\
If Len(Trim(testSt r)) = 0 Then
' 0 length or null string
Else
' string is initialised and not empty
End if
///

--

(O)enone
Oct 25 '06 #5
The code you have should work just fine. There are other ways to do it as
well though:

1. Use the VB.NET "feature" that null and empty string are considered equal
when using the = operator, i.e the following statement:
If thisString = "" Then
returns true if thisString is either empty *or* null (beware, this does not
work in C#)
2. Use the new String.IsNullOr Empty method (requires .NET 2.0)

/claes

"Neville Lang" <neville@MAPS_O Nnjlsoftware.co mwrote in message
news:uc******** ******@TK2MSFTN GP03.phx.gbl...
Hi all,

I am having a memory blank at the moment. I have been writing in C# for a
number of years and now need to do something in VB.NET, so forgive me such
a primitive question.

In C#, I test whether a string has a value or not by the following syntax:

if (thisString.Tri m() == "")
{
// if true
something()
}
else
{
// if false
somethingelse()
}

Now, in VB.NET an equivalent does not seem to work:

If thisString.Trim () = "" Then
' if true
something()
Else
' if false
somethingelse()
End If

I cannot get past a False return for the VB comparison. What is the best
method for testing whether a string is empty or null?
Regards,
Neville Lang


Oct 25 '06 #6
Regarding your first point, VB is even stranger than that:
A string set to Nothng (or uninitialized) is regarded as equal to an empty
string, but an empty string is *not* regarded as equal to Nothing. C# is
consistent in this respect: a null string is a null string and an empty
string is an empty string.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"Claes Bergefall" wrote:
The code you have should work just fine. There are other ways to do it as
well though:

1. Use the VB.NET "feature" that null and empty string are considered equal
when using the = operator, i.e the following statement:
If thisString = "" Then
returns true if thisString is either empty *or* null (beware, this does not
work in C#)
2. Use the new String.IsNullOr Empty method (requires .NET 2.0)

/claes

"Neville Lang" <neville@MAPS_O Nnjlsoftware.co mwrote in message
news:uc******** ******@TK2MSFTN GP03.phx.gbl...
Hi all,

I am having a memory blank at the moment. I have been writing in C# for a
number of years and now need to do something in VB.NET, so forgive me such
a primitive question.

In C#, I test whether a string has a value or not by the following syntax:

if (thisString.Tri m() == "")
{
// if true
something()
}
else
{
// if false
somethingelse()
}

Now, in VB.NET an equivalent does not seem to work:

If thisString.Trim () = "" Then
' if true
something()
Else
' if false
somethingelse()
End If

I cannot get past a False return for the VB comparison. What is the best
method for testing whether a string is empty or null?
Regards,
Neville Lang


Oct 25 '06 #7
Actually it does (at least in 2.0). All of the following evaluate to True:

Dim s1 As String = ""
Dim s2 As String = Nothing
Dim b1 As Boolean = (s1 = Nothing)
Dim b2 As Boolean = (s2 = Nothing)
Dim b3 As Boolean = (s1 = "")
Dim b4 As Boolean = (s2 = "")

You have to use String.Equals to distinguish between Nothing and an empty
string. Personally I find the VB.NET behaviour annoying and would prefer
that it worked the same as in C#

/claes

"David Anton" <Da********@dis cussions.micros oft.comwrote in message
news:63******** *************** ***********@mic rosoft.com...
Regarding your first point, VB is even stranger than that:
A string set to Nothng (or uninitialized) is regarded as equal to an empty
string, but an empty string is *not* regarded as equal to Nothing. C# is
consistent in this respect: a null string is a null string and an empty
string is an empty string.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"Claes Bergefall" wrote:
>The code you have should work just fine. There are other ways to do it as
well though:

1. Use the VB.NET "feature" that null and empty string are considered
equal
when using the = operator, i.e the following statement:
If thisString = "" Then
returns true if thisString is either empty *or* null (beware, this does
not
work in C#)
2. Use the new String.IsNullOr Empty method (requires .NET 2.0)

/claes

"Neville Lang" <neville@MAPS_O Nnjlsoftware.co mwrote in message
news:uc******* *******@TK2MSFT NGP03.phx.gbl.. .
Hi all,

I am having a memory blank at the moment. I have been writing in C# for
a
number of years and now need to do something in VB.NET, so forgive me
such
a primitive question.

In C#, I test whether a string has a value or not by the following
syntax:

if (thisString.Tri m() == "")
{
// if true
something()
}
else
{
// if false
somethingelse()
}

Now, in VB.NET an equivalent does not seem to work:

If thisString.Trim () = "" Then
' if true
something()
Else
' if false
somethingelse()
End If

I cannot get past a False return for the VB comparison. What is the
best
method for testing whether a string is empty or null?
Regards,
Neville Lang




Oct 26 '06 #8
Claes,
You have to use String.Equals to distinguish between Nothing and an empty
string.
Close! ;-)

I would recommend using "Is" to distinguish between Nothing and an empty
string.
Dim b5 As Boolean = (s1 Is Nothing)
Dim b6 As Boolean = (s2 Is Nothing)
One might consider these two, however they won't always return true:
Dim b7 As Boolean = (s1 Is "")
Dim b8 As Boolean = (s2 Is "")
Depending on how s1 & s2 got to be an empty string will change what the last
two return.

For example:

s2 = " ".Trim()

Will cause (s2 Is "") to fail, as " ".Trim() returns a new string,
where as "" returns an interned string...

Remember that = does value equality ("" & Nothing) are considered to have
the same value.

Where as Is does reference equality ("", " ".Trim() & Nothing) are
distinct object references.
Personally I find the VB.NET behavior annoying and would prefer that it
worked the same as in C#
Most of the time I agree, there are times though where I prefer the C#
behavior.

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Claes Bergefall" <lo*****@nospam .nospamwrote in message
news:%2******** **********@TK2M SFTNGP02.phx.gb l...
Actually it does (at least in 2.0). All of the following evaluate to True:

Dim s1 As String = ""
Dim s2 As String = Nothing
Dim b1 As Boolean = (s1 = Nothing)
Dim b2 As Boolean = (s2 = Nothing)
Dim b3 As Boolean = (s1 = "")
Dim b4 As Boolean = (s2 = "")

You have to use String.Equals to distinguish between Nothing and an empty
string. Personally I find the VB.NET behaviour annoying and would prefer
that it worked the same as in C#

/claes

"David Anton" <Da********@dis cussions.micros oft.comwrote in message
news:63******** *************** ***********@mic rosoft.com...
>Regarding your first point, VB is even stranger than that:
A string set to Nothng (or uninitialized) is regarded as equal to an
empty
string, but an empty string is *not* regarded as equal to Nothing. C# is
consistent in this respect: a null string is a null string and an empty
string is an empty string.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"Claes Bergefall" wrote:
>>The code you have should work just fine. There are other ways to do it
as
well though:

1. Use the VB.NET "feature" that null and empty string are considered
equal
when using the = operator, i.e the following statement:
If thisString = "" Then
returns true if thisString is either empty *or* null (beware, this does
not
work in C#)
2. Use the new String.IsNullOr Empty method (requires .NET 2.0)

/claes

"Neville Lang" <neville@MAPS_O Nnjlsoftware.co mwrote in message
news:uc****** ********@TK2MSF TNGP03.phx.gbl. ..
Hi all,

I am having a memory blank at the moment. I have been writing in C#
for a
number of years and now need to do something in VB.NET, so forgive me
such
a primitive question.

In C#, I test whether a string has a value or not by the following
syntax:

if (thisString.Tri m() == "")
{
// if true
something()
}
else
{
// if false
somethingelse()
}

Now, in VB.NET an equivalent does not seem to work:

If thisString.Trim () = "" Then
' if true
something()
Else
' if false
somethingelse()
End If

I cannot get past a False return for the VB comparison. What is the
best
method for testing whether a string is empty or null?
Regards,
Neville Lang



Oct 26 '06 #9
Yes, but try:
Dim s1 As String = ""
Dim b1 As Boolean = (s1 Is Nothing)

b1 evaluates to false. Isn't VB fun?!
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"Claes Bergefall" wrote:
Actually it does (at least in 2.0). All of the following evaluate to True:

Dim s1 As String = ""
Dim s2 As String = Nothing
Dim b1 As Boolean = (s1 = Nothing)
Dim b2 As Boolean = (s2 = Nothing)
Dim b3 As Boolean = (s1 = "")
Dim b4 As Boolean = (s2 = "")

You have to use String.Equals to distinguish between Nothing and an empty
string. Personally I find the VB.NET behaviour annoying and would prefer
that it worked the same as in C#

/claes

"David Anton" <Da********@dis cussions.micros oft.comwrote in message
news:63******** *************** ***********@mic rosoft.com...
Regarding your first point, VB is even stranger than that:
A string set to Nothng (or uninitialized) is regarded as equal to an empty
string, but an empty string is *not* regarded as equal to Nothing. C# is
consistent in this respect: a null string is a null string and an empty
string is an empty string.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"Claes Bergefall" wrote:
The code you have should work just fine. There are other ways to do it as
well though:

1. Use the VB.NET "feature" that null and empty string are considered
equal
when using the = operator, i.e the following statement:
If thisString = "" Then
returns true if thisString is either empty *or* null (beware, this does
not
work in C#)
2. Use the new String.IsNullOr Empty method (requires .NET 2.0)

/claes

"Neville Lang" <neville@MAPS_O Nnjlsoftware.co mwrote in message
news:uc******** ******@TK2MSFTN GP03.phx.gbl...
Hi all,

I am having a memory blank at the moment. I have been writing in C# for
a
number of years and now need to do something in VB.NET, so forgive me
such
a primitive question.

In C#, I test whether a string has a value or not by the following
syntax:

if (thisString.Tri m() == "")
{
// if true
something()
}
else
{
// if false
somethingelse()
}

Now, in VB.NET an equivalent does not seem to work:

If thisString.Trim () = "" Then
' if true
something()
Else
' if false
somethingelse()
End If

I cannot get past a False return for the VB comparison. What is the
best
method for testing whether a string is empty or null?
Regards,
Neville Lang




Oct 26 '06 #10

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

Similar topics

9
3729
by: R.Z. | last post by:
i was wondering whether it pays off in terms of memory use to maintain lots of empty deques (it would be convenient for my algorithms but memory use is more important). and does the size of a deque depends on the size of its members even if the deque is empty? is there at all a way to check out how much memory my deque occupies? i've read that the sizeof operator cannot be used with dynamically allocated arrays so i figured it wouldn't give...
1
2394
by: Monkey Steve | last post by:
Hi, I'm writing a website in asp.net(vb) with an access database behind it. As part of the site I have a table which has stores maximum and minimum values. These are integers at present. However - I need to change them to strings so that I can store the string "unknown" if these are not known. Problem is that my comparisons >= etc then won't work properly. Because of the way I am using the db I cannot have a pointer value, eg
7
1799
by: Clinton Pierce | last post by:
I'm calling a method in another assembly that's supposed to return a string, and possibly that string might be empty. The code looks something like this: string d = r.Field("date").ToString(); Console.WriteLine(count++ + " '" + d + "' --> " + d.Length); Sometimes "d" should be empty, and sometimes it's a date string. And in the VS.NET watch window, it's indeed empty. Except that my diagnostic line sometimes prints just the number...
4
2994
by: Sjaakie | last post by:
I need to replace parts of a string, in a collection deserialized from an XML file, with values from another collection. Is there another, more clever/faster/better, method than the loops below? TIA foreach (StringObject x in (StringObjectCollection)xc) { foreach (ReplacementStringObject r in
5
2499
by: Dimitri Furman | last post by:
SQL Server 2000 SP4. Hoping to find a logical explanation for a certain behavior. Consider this script: USE pubs GO IF 1 = ALL (SELECT contract FROM dbo.authors WHERE state = 'CA') PRINT 'TRUE'
12
2835
by: Merk | last post by:
What are some alternatives to using .ToUpper() to perform case insensitive string comparisons? The reason I'm asking is that I'm comparing strings in a long loop, looking for equality; and I want for this loop to run as fast as possible. So I'm looking for a method that would be faster than .ToUpper(). Thanks!
14
13484
by: Steve Bergman | last post by:
I'm looking for a module to do fuzzy comparison of strings. I have 2 item master files which are supposed to be identical, but they have thousands of records where the item numbers don't match in various ways. One might include a '-' or have leading zeros, or have a single character missing, or a zero that is typed as a letter 'O'. That kind of thing. These tables currently reside in a mysql database. I was wondering if there is a...
0
10581
NeoPa
by: NeoPa | last post by:
ANSI-89 v ANSI-92 Before we get into all the various types of pattern matching that can be used, there are two ANSI standards used for the main types of wildcard matching (matching zero or more characters or simply matching a single character) : ANSI-89 - Mainly used only by Jet / ACE SQL ANSI-92 - Mainly used by SQL Server and other grown-up products In the later versions of Access it is now possible to select ANSI-92 compatibility as an...
35
3660
by: Smithers | last post by:
I have been told that it is a good idea to *always* declare string variables with a default value of string.Empty - for cases where an initial value is not known... like this: string myString = string.Empty; // do this string myString; // do not do this Questions 1. Is that a good rule? 2. If so, why? If not, why not?
0
8581
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8514
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9050
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6439
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.