Freigeben über


String.Empty-Feld

Stellt die leere Zeichenfolge dar. Dieses Feld ist schreibgeschützt.

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

Syntax

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

value = String.Empty
public static readonly string Empty
public:
static initonly String^ Empty
public static final String Empty
public static final var Empty : String

Hinweise

Der Wert dieses Feldes ist die Zeichenfolge "" mit der Länge 0.

Beispiel

In den folgenden Beispielen wird die Verwendung des Empty-Felds veranschaulicht.

Im ersten Beispiel wird die Empty-Zeichenfolge als Standardwert zurückgegeben, wenn der Wert eines anderen Feldes NULL (Nothing in Visual Basic) ist.

   Dim myBinding As DataBinding = DataBindings("Text")
   If Not (myBinding Is Nothing) Then
      Return myBinding.Expression
   End If
   Return [String].Empty
End Get
DataBinding myBinding = DataBindings["Text"];
if (myBinding != null)
{
   return myBinding.Expression;
}
return String.Empty;
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("");

Im zweiten Beispiel wird mithilfe der Empty-Zeichenfolge eine Teilzeichenfolge in Compare getestet.

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";
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";
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.

Im dritten Beispiel wird die Empty-Zeichenfolge im Entscheidungsblock des XPathNavigator-Objekts verwendet, um Entscheidungen zur XML-Analyse zu treffen.

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.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);
    }
}   
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

Plattformen

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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

String-Klasse
String-Member
System-Namespace