ASP.net String.IsNullOrEmpty method _ Practical skills

Source: Internet
Author: User
Tags mscorlib
This method is new in the. NET Framework version 2.0.
Indicates whether the specified string object is a null reference (Nothing in Visual Basic) or a Empty string.
This method in Vb,vb.net, c#,c++,jscript,j#, a very good very powerful method.

String.Empty Field
Represents an empty string. This field is read-only.
Namespaces: System
Assembly: mscorlib (in mscorlib.dll)
Grammar
Visual Basic (Declaration)
Public Shared ReadOnly Empty as String
Visual Basic (usage)
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
Note
The value of this field is a zero-length string "".
Example
The following code example shows how to use the Empty field.
In the first example, if the value of another field is a null reference (Nothing in Visual Basic), the Empty string is returned as the default value.
Visual Basic Copy Code
Copy Code code as follows:

Dim mybinding as DataBinding = DataBindings ("Text")
If not (mybinding are nothing) Then
Return mybinding.expression
End If
return [String]. Empty
End Get

C # Copy Code
Copy Code code as follows:

DataBinding mybinding = databindings["Text"];
if (mybinding!= null)
{
return mybinding.expression;
}
return String.Empty;

C + + Replication code
Copy Code code as follows:

databinding^ mybinding = databindings["Text"];
if (mybinding!= nullptr)
{
Return mybinding->expression;
}
return string::empty;

J # Replication Code
Copy Code code as follows:

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 the substring.
Visual Basic Copy Code
Copy Code code as follows:

Dim myString as String = "abc"
Dim test1 as Boolean = String.Compare (mystring.substring (2, 1), "c") = 0 ' is true.
Mystring.substring (3, 1) ' This throws ArgumentOutOfRangeException.
Dim test2 as Boolean = String.Compare (mystring.substring (3, 0), String.Empty) = 0 ' is true.

C # Copy Code
Copy Code code as follows:

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 + + Replication code
Copy Code code as follows:

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 # Replication Code
Copy Code code as follows:

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 Copy Code
Copy Code code as follows:

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, a Empty string is used in the decision block of the XPathNavigator object to make decisions about XML parsing.
Visual Basic Copy Code
Copy Code code as follows:

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 ())
Todo
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 # Copy Code
Copy Code code as follows:

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 + + Replication code
Copy Code code as follows:

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 ())
{
Todo
{
Recursivewalk (NAV);
}
while (Nav->movetonext ());
Nav->movetoparent ();
if (Nav->nodetype = = xpathnodetype::element)
Console::WriteLine ("</{0}>", nav->name);
}
}

J # Replication Code
Copy Code code as follows:

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

Platform
Windows 98, Windows SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartpho NE, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Start ER Edition
The. NET Framework does not provide support for all versions of each platform. For a list of supported versions, see System requirements.
Version information
The. NET Framework
Supported by the following versions: 2.0, 1.1, 1.0
The. NET Compact Framework
Supported by the following version: 2.0, 1.0
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.