Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Empty Field
Collapse the table of content
Expand the table of content

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
Not applicable.

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 Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2015 Microsoft