473,387 Members | 3,750 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,387 software developers and data experts.

Check if String is Empty

Hello,

I need to check if a string is empty.

Should I use:

String.IsNullOrEmpty(MyString) or MyString Is Nothing?

What is the difference?

Thanks,
Miguel

Jan 16 '07 #1
2 16282
Hello,
>
I need to check if a string is empty.

Should I use:

String.IsNullOrEmpty(MyString) or MyString Is Nothing?

What is the difference?

Thanks,
Miguel
A string variable can point to Nothing (ie be unassigned). Although it is
possible to have Nothing string vars, you are supposed to give empty string
values the special value of String.Empty rather than leaving them point to
Nothing.

Dim myString as String = Nothing ' Wrong
Dim myString as String = String.Empty ' Right

The "MyString Is Nothing" only checks of the string is pointing to Nothing
(ir not initialised). The IsNullOrEmpty checks if the string is Nothing, or
if it contains String.Empty as a value.
Jan 16 '07 #2
I like

If (stringName.Length 0) Then
'String is not nothing
Else
'String is empty
End If

The above is relatively fast. You can also Trim() if you are not sure the
string is not full of spaces:

If (textBoxName.Text.Trim().Length 0) Then
End If

There are a variety of ways to do this:

If Not (stringName = String.Empty) Then

etc.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

********************************************
Think outside the box!
********************************************
"shapper" <md*****@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
Hello,

I need to check if a string is empty.

Should I use:

String.IsNullOrEmpty(MyString) or MyString Is Nothing?

What is the difference?

Thanks,
Miguel
Jan 16 '07 #3

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

Similar topics

6
by: Ole Hanson | last post by:
Hi I am wondering if there is any programmatic difference between the String.Empty and the "" for declaring an empty string? Furthermore, is there any performance penalty by doing the one over...
8
by: monu | last post by:
hi can u write me Why string.Empty is better then "" bye
4
by: AVL | last post by:
Hi, I read that to check for empty strings , comparing strings using String.Length == 0 is faster than String.Empty or "" . Is it true...? If so, how?
9
by: Søren M. Olesen | last post by:
Hi Can someone tell me how to implement something similar to String.Empty ?? Given a class called myValue Public class myValue private id as integer private value as string end class
6
by: Charlie | last post by:
Hi: Is there any difference between string.Empty and String.Empty? And what is the benefit of using it over "". Thanks, Charlie
26
by: anonieko | last post by:
In the past I always used "" everywhere for empty string in my code without a problem. Now, do you think I should use String.Empty instead of "" (at all times) ? Let me know your thoughts.
35
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 =...
21
by: Sami | last post by:
string = "" or string = string.Empty? should is the proper way? Sami
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.