String.Empty Field
Represents the empty string. This field is read-only.
[Visual Basic] Public Shared ReadOnly Empty As String [C#] public static readonly string Empty; [C++] public: static String* Empty; [JScript] public static var Empty : String;
Remarks
The value of this field is the zero-length string, "".
Example
The following 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).
[Visual Basic] Dim myBinding As DataBinding = DataBindings("Text") If Not (myBinding Is Nothing) Then Return myBinding.Expression End If Return [String].Empty End Get [C#] DataBinding myBinding = DataBindings["Text"]; if (myBinding != null) { return myBinding.Expression; } return String.Empty; [C++] DataBinding* myBinding = DataBindings->Item[S"Text"]; if (myBinding != 0) { return myBinding->Expression; } return String::Empty;
In the second example, the Empty string is used in Compare to test a substring.
[Visual Basic] 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. [C#] String myString = "abc"; bool test1 = String.Compare(myString.Substring(2, 1), "c") == 0; // This is true. myString.Substring(3, 1); // This throws ArgumentOutOfRangeException. bool test2 = String.Compare(myString.Substring(3, 0), String.Empty) == 0; // This is true. [C++] String *myString = L"abc"; bool test1 = String::Compare(myString->Substring(2,1), L"c") == 0; // This is true. myString->Substring(3,1); // This throws ArgumentOutOfRangeException. bool test2 = String::Compare(myString->Substring(3,0), String::Empty) == 0; // This is true. [JScript] 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 to make decisions about XML parsing.
[Visual Basic] 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 [C#] public static void RecursiveWalk(XPathNavigator nav) { switch (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("\t"+ nav.NamespaceURI); break; case XPathNodeType.Text: Console.WriteLine("\t" + nav.Value); break; } if ( nav.MoveToFirstChild() ) { do{ RecursiveWalk(nav); } while ( nav.MoveToNext() ); nav.MoveToParent(); if (nav.NodeType == XPathNodeType.Element) Console.WriteLine("</{0}>", nav.Name); } } [C++] static void RecursiveWalk(XPathNavigator * nav) { switch (nav -> NodeType) { case XPathNodeType::Element: if (nav -> Prefix == String::Empty) Console::WriteLine(S"< {0}>", nav -> LocalName); else Console::Write(S"< {0}: {1}>", nav -> Prefix, nav -> LocalName); Console::WriteLine(S"\t {0}", nav -> NamespaceURI); break; case XPathNodeType::Text: Console::WriteLine(S"\t {0}", nav -> Value); break; } if (nav -> MoveToFirstChild()) { do { RecursiveWalk(nav); } while (nav -> MoveToNext()); nav -> MoveToParent(); if (nav -> NodeType == XPathNodeType::Element) Console::WriteLine(S"</ {0}>", nav -> Name); } }
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard