c#中的String.IsNullOrWhiteSpace(aa) 是否等同于Javaabo中的aaa!=null &&aa!="",如果不是的话,等同于什么

以前刚入行的时候判断字符串的时候用
string a="a";
后来发现了String.IsNullOrEmpty感觉方便了好多,但是后来发现如果字符串的空白String a=" &";IsNullOrEmpty就没法判断了,于是我今天发现了String.IsNullOrWhiteSpace,此方法只在framework4.0以上才能使用,官方的解释是:指示指定的字符串是&、空还是仅由空白字符组成。
/zh-cn/library/system.string.isnullorwhitespace(v=vs.100).aspx
string a = null;
string b = string.E
string c = "";
string d = "
Console.WriteLine("a:{0};\r\n b:{1};\r\n c:{2};\r\n d:{3};\r\n", a, b, c, d);
if (string.IsNullOrEmpty(a))
Console.WriteLine("a");
if (string.IsNullOrEmpty(b))
Console.WriteLine("b");
if (string.IsNullOrEmpty(c))
Console.WriteLine("c");
if (string.IsNullOrEmpty(d))
Console.WriteLine("d");
if (string.IsNullOrWhiteSpace(a))
Console.WriteLine("a1");
if (string.IsNullOrWhiteSpace(b))
Console.WriteLine("b1");
if (string.IsNullOrWhiteSpace(c))
Console.WriteLine("c1");
if (string.IsNullOrWhiteSpace(d))
Console.WriteLine("d1");
Console.Read();
执行结果:
由此可见当用IsNullOrEmpty时,d是没有输出来的,但是string.IsNullOrWhiteSpace却可以,如果执意要用前者又要判断空白的话,不妨与Trim组合使用。
阅读(...) 评论()C#string详解 - 暮雨冰蓝 - 推酷
C#string详解 - 暮雨冰蓝
平时用的最多的莫过于string了,但有时遇到的一些问题,不仔细想还真容易出错,今天我就来总结一下string的用法。
1.string是一个引用类型,平时我们比较string对象,比较的是对象的值而不是对象本身
string strA=&abcde&;
string strB=&abc&;
string strC=&de&;
Console.WriteLine(strA == (strB+strC));//true
Console.WriteLine((object)strA == (object)(strB+strC));//false
因为字符串内容相同但引用的不是同一个实例
2.string对象是不可修改的
string strA=&abcde&;
strA=&aaaaa&;
从表面上看似修改了strA的内容,事实上&abcde&没有被修改,而是从新创建了一个对象&aaaaa&,然后把该对象的引用赋给strA,最后&abcde&会被作为垃圾回收。
3.string的创建
直接赋值:string strA=&abcde&;//创建一个内容为abcde的string对象,然后将该对象的引用赋给strA
构造: char[] arr={'a','b','c','d','e'};
string strA=new string(arr);//这里只列举一种
注意:没有String str=new String(&abcde&);这样的构造,
<span data-guid="20f4cb61fb9b73953cbbfb0d0f7ebadd" data-source="The string type represents a sequence of zero or more Unicode characters.">
<span data-guid="6996acf94e1d69b19c00" data-source="string is an alias for
in the .NET Framework.">string&是 .NET Framework 中
String的别名。
3.string参数传递
string是引用类型,我们试图在一个函数里改变这个值
static void Main(string[] args)
string strA = &abcde&;
Deal(strA);
Console.WriteLine(strA);
Console.ReadLine();
static void Deal(string str)
str = str.Substring(0, 2);
结果:abcde
原因:通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值。但无法更改引用本身的值,通过ref关键字传递参数可解决该问题。
static void Main(string[] args)
string strA = &abcde&;
Deal(strA);
Console.WriteLine(ref strA);
Console.ReadLine();
static void Deal(ref string str)
str = str.Substring(0, 2);
此时传递的是引用本身,而不是副本
4.null 字符串和空字符串
null 字符串:没有分配内存;空字符串分配了内存,但内存里面没有数据.
static void Main(string[] args)
string strA = &1&;
string strB = string.E
string strC = null;
Console.WriteLine(int.Parse(strA));//正确
Console.WriteLine(int.Parse(strB));//输入字符串的格式不正确
Console.WriteLine(strC.ToString());//未将对象引用设置到对象的实例。
Console.ReadLine();
内置方法字符串是否为&null&或为空:
IsNullOrEmpty等同于&if (str == null || str.Equals(String.Empty))
IsNullOrWhiteSpace等同于 &if (str == null || str.Equals(String.Empty) || str.Trim().Equals(String.Empty))&
5.StringBuilder
string strA=&abc&
for(int i=0;i&10000;i++)
strA+=&abc&;
Consolse.WriteLine(strA);
尽管该代码会出现以使用字符串串联来将新的字符追加到命名为&strA 的现有字符串,它实际上会为每个串联操作创建新的&String&对象。大大的降低了性能。可使用&StringBuilder&类代替String&类多次更改字符串值,
<span data-guid="46c4ec7a6cc06df97b175cb" data-source="An ordinal operation acts on the numeric value of each
<span data-guid="46c4ec7a6cc06df97b175cb" data-source="An ordinal operation acts on the numeric value of each
StringBuilder&对象是可变的,当进行追加或删除字符串中的子字符串时,不会创建新的对象,而是在原来的对象上进行修改。&
<span data-guid="f7fdafa6a3e420a84e90" data-source="A culture-sensitive operation acts on the value of the String object, and takes culture-specific casing, sorting, formatting, and parsing rules into account.">完成&StringBuilder&对象的值的修改后,可以调用其&StringBuilder.ToString&方法将其转换为字符串
StringBuilder strA=new StringBuilder();
for(int i=0;i&10000;i++)
strA.Append(&abc&);
Consolse.WriteLine(strA.ToString());
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致25631人阅读
[ 03 ] C#(136)
[ 21 ] Winform(18)
[ 24 ] ASP.NET(85)
.NET(193)
由于原来一直都没注意到这两个方法,一直使用string.IsNullOrEmpty,当看到string.IsNullOrWhiteSpace时,而且在微软人员开发的项目中经常使用时才注意到,查了一下MSDN,记一下免得以后忘记。
string.IsNullOrEmpty
都知道,这个功能是判断字符串是否为:null或者string.Empty。如果是如&\t&这样的字符就返回false了,为了达到判断过滤这些功能,就要使用Trim()和Length属性帮忙,判断是否长度为零,于是乎就产生了如下的方法。
string.IsNullOrWhiteSpace
这个是判断所有空白字符,功能相当于string.IsNullOrEmpty和str.Trim().Length总和,他将字符串给Char.IsWhiteSpace为ture的任何字符都将是正确的。根据MSDN的说明,这个方法会比调用上述两个方法的性能更高而且简洁,所以在判断这个功能时,推荐使用。
using System;
public class Example
public static void Main()
string[] values = { null, String.Empty, &ABCDE&,
new String(' ', 20), &
new String('\u2000', 10) };
foreach (string value in values)
Console.WriteLine(String.IsNullOrWhiteSpace(value));
// The example displays the following output:
以上就是代码执行效果,至于性能就听微软的吧,不过string.IsNullOrEmpty和string.IsNullOrWhiteSpace相比,肯定是前面一个性能更高【没有测试过,如果有哪位测试过的可以留言告诉我哦,谢谢!】,所以还是要选择性使用的。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1258858次
积分:13190
积分:13190
排名:第1099名
原创:182篇
转载:618篇
评论:65条
(1)(4)(11)(9)(1)(1)(10)(17)(3)(14)(5)(7)(3)(8)(3)(12)(3)(3)(5)(7)(1)(19)(11)(11)(8)(4)(5)(2)(4)(12)(15)(4)(13)(21)(18)(66)(45)(25)(17)(12)(6)(13)(22)(33)(4)(29)(22)(12)(7)(28)(7)(11)(6)(7)(5)(10)(4)(8)(24)(6)(6)(16)(16)(1)(1)(2)(6)(6)(6)(4)(25)(2)(5)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'}

我要回帖

更多关于 excel中aa是什么意思 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信