欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net String.IsNullOrEmpty 方法

程序员文章站 2024-03-11 21:18:37
此方法在 .net framework 2.0 版中是新增的。 指示指定的 string 对象是 空引用(在 visual basic 中为 nothing) 还是 emp...
此方法在 .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