Asp.net string. isnullorempty Method

Source: Internet
Author: User
Tags mscorlib

This method is added in. NET Framework 2.0.
Indicates whether the specified String object is a null reference (nothing in Visual Basic) or an empty string.
This method is available in VB, VB. NET, C #, C ++, JScript, and J #. It is a very powerful method.

string. Empty field
indicates an empty string. This field is read-only.
namespace: System
Program set: mscorlib (in mscorlib. DLL)
syntax
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
remarks
the value of this field is a zero-length string "".
example
the Code below demonstrates how to use the empty field.
In the first example, if the value of another field is null reference (nothing in Visual Basic), the empty string is returned as the default value.
copy the code in Visual Basic copy Code the code is as follows: dim mybinding as databinding = databindings (" text ")
if not (mybinding is nothing) then
return mybinding. expression
end if
return [String]. empty
end get

C # copy codeCopy codeThe Code is as follows: databinding mybinding = databindings ["text"];
If (mybinding! = NULL)
{
Return mybinding. expression;
}
Return string. empty;

C ++ copy codeCopy codeThe Code is as follows: databinding ^ mybinding = databindings ["text"];
If (mybinding! = Nullptr)
{
Return mybinding-> expression;
}
Return string: empty;

J # copy codeCopy codeThe Code is as follows: databinding mybinding = get_databindings (). get_item ("text ");
If (mybinding! = NULL ){
Return mybinding. get_expression ();
}
Return ("");

In the second example, compare uses the empty string to test the substring.
Copy code in Visual BasicCopy codeThe Code is as follows: 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 # copy codeCopy codeThe Code is 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 ++ copy codeCopy codeThe Code is 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 # copy codeCopy codeThe Code is 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 the code is 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, the decision block of the xpathnavigator object uses the empty string to make decisions on XML analysis.
copy the code in Visual Basic copy Code the code is 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 ()
DO
recursivewalk (NAV)
loop while (NAV. movetonext ()
nav. movetoparent ()
If (NAV. nodetype = xpathnodetype. element)
console. writeline (" ", Nav. name)
end if
end sub

C # copy the Code copy Code the code is as follows: public static void recursivewalk (xpathnavigator NAV)
{< br> 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;
}< br> If (NAV. movetofirstchild ()
{< br> do {
recursivewalk (NAV);
}while (NAV. movetonext ();
nav. movetoparent ();
If (NAV. nodetype = xpathnodetype. element)
console. writeline (" ", Nav. name);
}< BR >}

C ++ copy Code copy Code the code is as follows: static void recursivewalk (xpathnavigator ^ NAV)
{< br> switch (NAV-> nodetype)
{< br> 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;
}< br> If (NAV-> movetofirstchild ()
{< br> DO
{< br> recursivewalk (NAV );
}< br> while (NAV-> movetonext ();
nav-> movetoparent ();
If (NAV-> nodetype = xpathnodetype:: element)
Console: writeline (" ", NAV-> name);
}< BR >}

J # copy the Code copy Code the code is as follows: public static void recursivewalk (xpathnavigator NAV)
{< br> switch (NAV. get_nodetype () {
case xpathnodetype. element:
If (NAV. get_prefix (). equals ("") {
console. writeline ("<{0}>", Nav. get_localname ();
}< br> else {
console. write ("<{0 }:{ 1}>", Nav. get_prefix (),
nav. get_localname ();
}< br> console. writeline ("\ t" + nav. get_namespaceuri ();
break;
case xpathnodetype. text:
console. writeline ("\ t" + nav. get_value ();
break;
}< br> If (NAV. movetofirstchild () {
do {
recursivewalk (NAV);
}while (NAV. movetonext ();
nav. movetoparent ();
If (NAV. get_nodetype (). equals (xpathnodetype. element) {
console. writeline (" ", Nav. get_name ();
}< BR >}// recursivewalk

platform
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 does not support all versions of each platform. For a list of supported versions, see system requirements.
Version Information
.. NET Framework
supported by the following versions: 2.0, 1.1, and 1.0
. NET Compact framework
supported by the following versions: 2.0 and 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.