String.Empty Field
.NET Framework 3.0
Represents the empty string. This field is read-only.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
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).
DataBinding^ myBinding = DataBindings[ "Text" ]; if ( myBinding != nullptr ) { return myBinding->Expression; } return String::Empty;
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.
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.
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.
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 {0}", nav->NamespaceURI ); break; case XPathNodeType::Text: Console::WriteLine( "\t {0}", nav->Value ); break; } if ( nav->MoveToFirstChild() ) { do { RecursiveWalk( nav ); } while ( nav->MoveToNext() ); nav->MoveToParent(); if ( nav->NodeType == XPathNodeType::Element ) Console::WriteLine( "</ {0}>", nav->Name ); } }
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.Community Additions
ADD
Show: