Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.Empty Field

Represents the empty string. This field is read-only.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

'Declaration
Public Shared ReadOnly Empty As String
'Usage
Dim value As String

value = String.Empty

public static final String Empty
public static final var Empty : String

The value of this field is the zero-length string, "".

The following code examples demonstrate how the Empty field can be used.

In the first example, the Empty string is returned as a default value if the value of another field is a null reference (Nothing in Visual Basic).

   Dim myBinding As DataBinding = DataBindings("Text")
   If Not (myBinding Is Nothing) Then
      Return myBinding.Expression
   End If
   Return [String].Empty
End Get

DataBinding myBinding = get_DataBindings().get_Item("Text");
if (myBinding != null) {
    return myBinding.get_Expression();
}
return("");

In the second example, the Empty string is used in Compare to test a substring.

Dim myString As String = "abc"
Dim test1 As Boolean = String.Compare(myString.Substring(2, 1), "c") = 0 ' This is true.
myString.Substring(3, 1) ' This throws ArgumentOutOfRangeException.
Dim test2 As Boolean = String.Compare(myString.Substring(3, 0), String.Empty) = 0 ' This is true.

String myString = "abc";
// This is true.
boolean test1 = String.Compare(myString.Substring(2, 1), "c") == 0;        
myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.
// This is true.
boolean test2 = String.Compare(myString.Substring(3, 0), " ") == 0;        

var myString : String = "abc";
var test1 : boolean = String.Compare(myString.Substring(2, 1), "c") == 0; // This is true.
myString.Substring(3, 1);  // This throws ArgumentOutOfRangeException.
var test2 : boolean = String.Compare(myString.Substring(3, 0), String.Empty) == 0; // This is true.

In the third example, the Empty string is used in the decision block of the XPathNavigator object to make decisions about XML parsing.

public shared sub RecursiveWalk(nav as XPathNavigator)

   select case nav.NodeType
     case XPathNodeType.Element
        if (nav.Prefix=String.Empty)
          Console.WriteLine("<{0}>", nav.LocalName)
        else
          Console.Write("<{0}:{1}>", nav.Prefix, nav.LocalName)
          Console.WriteLine("  "+ nav.NamespaceURI)
        end if
     case XPathNodeType.Text
        Console.WriteLine("  " + nav.Value)
   end select

   if ( nav.MoveToFirstChild() )
      do
         RecursiveWalk(nav)
      loop while ( nav.MoveToNext() )

      nav.MoveToParent()
      if (nav.NodeType = XPathNodeType.Element)
        Console.WriteLine("</{0}>", nav.Name)
      end if
   end if

end sub

public static void RecursiveWalk(XPathNavigator nav)
{
    switch (nav.get_NodeType()) {
        case XPathNodeType.Element:
            if (nav.get_Prefix().Equals("")){
                Console.WriteLine("<{0}>", nav.get_LocalName());
            }
            else {
                Console.Write("<{0}:{1}>", nav.get_Prefix(),
                    nav.get_LocalName());
            }
            Console.WriteLine("\t" + nav.get_NamespaceURI());
            break;

        case XPathNodeType.Text:
            Console.WriteLine("\t" + nav.get_Value());
            break;
    }

    if (nav.MoveToFirstChild()) {
        do {
            RecursiveWalk(nav);
        } while (nav.MoveToNext());

        nav.MoveToParent();
        if (nav.get_NodeType() .Equals(XPathNodeType.Element)) {
            Console.WriteLine("</{0}>", nav.get_Name());
        }
    }
} //RecursiveWalk

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

Show:
© 2014 Microsoft