2

I've always use String.IsNullOrEmpty to check for a empty string. It recently come to my attention that a " " count as not a empty string. For example,

 Dim test As String = " "
    If String.IsNullOrEmpty(test) Then
        MessageBox.Show("Empty String")
    Else
        MessageBox.Show("Not Emtpy String")
    End If

It will show "Not Empty String". So how do we check for " " or " " in a string?

edit: I wasn't aware that " " count as character.

Noldorin
  • 144,213
  • 56
  • 264
  • 302
Jack
  • 9,843
  • 23
  • 78
  • 111

13 Answers13

13

Try this method to check for blank strings. It is different from the Trim() versions in that it does not allocate a new string. It also uses a more expanded notion of whitespace.

Public Function IsNullOrBlank(ByVal str as String) As Boolean
  if String.IsNullOrEmpty(str) Then
    Return True
  End IF
  For Each c In str
    IF Not Char.IsWhiteSpace(c) Then
      Return False
    End If
  Next
  Return True
End Function
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Ohh the top of your head do you know what the associated cost of using Trim vs this? – JoshBerke Apr 07 '09 at 15:17
  • @Josh no, that would require a profiler :). I imagine they are close to the same speed as they do roughly the same thing. The main difference is the allocation of the string – JaredPar Apr 07 '09 at 15:26
  • 1
    Trim() would at worst copy n-1 characters from your original string into a new one (if there was one leading or trailing white space). At best it would return the empty string (if it was already empty or blank) or the original string (if there were no leading or trailing white space). –  Apr 04 '11 at 17:34
7

String.IsNullOrWhiteSpace is in the BCL in .NET 4

Phil Devaney
  • 17,607
  • 6
  • 41
  • 33
6

An " " is an ASCII 32 value, it is no different from any other ASCII character except it "looks" blank.

Petras
  • 4,686
  • 14
  • 57
  • 89
3

The problem is you need to trim the string, however if you call trim() on a null string you will get an error.

string.IsNullOrEmpty(s.Trim())

This will error out.

You will need to do something like

if (Not string.IsNullOrEmpty(s) AndAlso s.Trim()!=string.Empty)

This will verify that the string isn't null or empty, if has something it will trim and then verify its not empty.

Edit

Thanks Slough for helping me with my VB syntax. I'm a C# guy need to brush up on vb's syntax

JoshBerke
  • 66,142
  • 25
  • 126
  • 164
1

In VB.NET you'll need to use a test like this:

If String.IsNullOrEmpty(test) OrElse String.IsNullOrEmpty(test.Trim()) Then

The OrElse prevents the NullException from occurring on the test.Trim()

Gavin Miller
  • 43,168
  • 21
  • 122
  • 188
1
public static bool IsNullOrWhite(string s)
{
  if (String.IsNullOrEmpty(s))
    return true;

  for (int i = 0; i < s.Length; i++)
    if (!Char.IsWhiteSpace(s, i))
      return false;

  return true;
}
François
  • 935
  • 1
  • 7
  • 11
0

Perhaps you just want to trim the string (of spaces) before checking it?

Dim test As String = " "
If test.Trim().Length = 0 Then // Best option as long as string is guaranteed not to be null.
//  If test = Nothing OrElse test.Trim().Length = 0 Then // If string can be null.
    MessageBox.Show("Empty String")
Else
    MessageBox.Show("Not Emtpy String")
End If
Noldorin
  • 144,213
  • 56
  • 264
  • 302
0

As was mentioned, calling Trim() will throw a NullReferenceException if the string is null. I sometimes use Regex.IsMatch(test, "^ +$") (hope I have the parameter order right) to test for one or more spaces. The ^ and $ make sure you're matching the entire string.

John M Gant
  • 18,970
  • 18
  • 64
  • 82
0

If you really need to treat strings containing only whitespace character the same as empty or null strings, then you could use an extension method like this one (sorry, it's in C#):

namespace ExtensionMethods
{
    public static class StringExtensions
    {
        public static bool IsNullOrEmptyOrWhitespace(this string str)
        {
            if (string.IsNullOrEmpty(str)) return true;
            return (str.Trim().Length == 0);
        }
    }
}

This allows you to write:

using ExtensionMethods;

string s1 = "  ";
string s2 = "some string";

s1.IsNullOrEmptyOrWhitespace(); //-> returns true
s2.IsNullOrEmptyOrWhitespace(); //-> returns false
M4N
  • 94,805
  • 45
  • 217
  • 260
0
If test Is Nothing OrElse test.Trim().Length = 0 Then ...
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
-1

You can add an extension method such as was discussed here and keep the same ease of use you previously had with IsNullOrEmpty().

Community
  • 1
  • 1
sipsorcery
  • 30,273
  • 24
  • 104
  • 155
  • Sigh... Mine is exactly the same answer as Martin's below. Cutting and pasting gets an up vote while providing a link gets a downvote. Must be someone using Lynx who gets irritated by hyperlinks. – sipsorcery Apr 07 '09 at 22:37
-1
If String.IsNullOrEmpty(str) OrElse str.Trim().Length = 0 Then
  ' ..
End If

The following may not work sometime if the str is null as calling a method on null will result in exception. If String.IsNullOrEmpty(str.Trim()) Then ' exception sometimes ... End If

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Niran
  • 1,106
  • 1
  • 8
  • 10
-2

Try

Dim test As String = " "
If String.IsNullOrEmpty(test.Trim()) Then
    MessageBox.Show("Empty String")
Else
    MessageBox.Show("Not Emtpy String")
End If

Hope that helps.

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
Stephen Newman
  • 1,977
  • 14
  • 18