程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net String.IsNullOrEmpty 方法

asp.net String.IsNullOrEmpty 方法

編輯:ASP.NET基礎
此方法在 .NET Framework 2.0 版中是新增的。
指示指定的 String 對象是 空引用(在 Visual Basic 中為 Nothing) 還是 Empty 字符串。
這個方法在VB,VB.Net, C#,C++,JScript,J#中都有,很好很強大的一個方法。

String.Empty 字段
表示空字符串。此字段為只讀。
命名空間:System
程序集:mscorlib(在 mscorlib.dll 中)
語法
Visual Basic(聲明)
Public Shared ReadOnly Empty As String
Visual Basic(用法)
Dim value As String
value = String.Empty
C#
public static readonly string Empty
C++
public:
static initonly String^ Empty
J#
public static final String Empty
JScript
public static final var Empty : String
備注
此字段的值為零長度字符串 ""。
示例
下面的代碼示例演示如何使用 Empty 字段。
在第一個示例中,如果另一個字段的值為 空引用(在 Visual Basic 中為 Nothing),則 Empty 字符串作為默認值返回。
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[ "Text" ];
if ( myBinding != nullptr )
{
return myBinding->Expression;
}
return String::Empty;

J# 復制代碼
復制代碼 代碼如下:
DataBinding myBinding = get_DataBindings().get_Item("Text");
if (myBinding != null) {
return myBinding.get_Expression();
}
return("");

在第二個示例中,Compare 中使用了 Empty 字符串來測試子字符串。
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 = "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.

J# 復制代碼
復制代碼 代碼如下:
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;

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.

在第三個示例中,XPathNavigator 對象的決策塊中使用了 Empty 字符串來作出有關 XML 分析的決策。
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( "< {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 );
}
}

J# 復制代碼
復制代碼 代碼如下:
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 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 並不是對每個平台的所有版本都提供支持。有關受支持版本的列表,請參見系統要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved