次の方法で共有


String.Empty フィールド

空の文字列を表します。このフィールドは読み取り専用です。

Public Shared ReadOnly Empty As String
[C#]
public static readonly string Empty;
[C++]
public: static String* Empty;
[JScript]
public static var Empty : String;

解説

このフィールドの値は、長さ 0 の文字列 "" です。

使用例

Empty フィールドの使用方法については、次のコード例を参照してください。

最初の例では、もう 1 つのフィールドの値が null 参照 (Visual Basic では Nothing) である場合に、 Empty 文字列が既定として返されます。

 
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;

2 番目の例では、 Empty 文字列を Compare で使用してサブ文字列をテストします。

 
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.

3 番目の例では、 Empty 文字列を XPathNavigator の判定ブロックで使用して XML 解析を判定します。

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

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

String クラス | String メンバ | System 名前空間