C# 字符串判空(isNullOrEmpty)

string str 

  1:string.IsNullOrEmpty == str  这种方法是我最喜欢用的,它不但一次性能判断"空的字符串变量",还能判断“值为空字符串的变量”,并且还可以让代码简洁美观。判断的效率也不算低。

  2:str.Length == 0 这种方式,我不怎么喜欢用,不推荐使用。在网上看和自己的实际测试,确实能证明这种判断方式的执行效率最高,但要使用它你必须保证字符串不null,如果为null就会报出异常,

  3.str == string.Empty 或 str == "" 这两种方式,我也不推荐使用,他只能判断“值为空字符串”的字符串变量,而且效率比较低

  4.str == null 这种方式我也不怎么推荐,原因和3一样。

下面摘自微软官网:

String.IsNullOrEmpty 方法

指示指定的字符串是 null 还是 Empty 字符串。

public static bool IsNullOrEmpty(	string value
)

参数

返回值

类型:System.Boolean
如果 value 参数为 null 或空字符串 (""),则为 true;否则为 false。

IsNullOrEmpty 是一种简便方法,它使您能够同时测试 String 是否为 null 或其值是否为 Empty。 它与下列代码等效:

result = s == null || s == String.Empty;

可以使用 IsNullOrWhiteSpace 方法测试字符串是否 null,其值为 String.Empty,或仅包含空白字符。

using System;class Sample 
{    public static void Main() 
    {    string s1 = "abcd";    string s2 = "";    string s3 = null;

    Console.WriteLine("String s1 {0}.", Test(s1));
    Console.WriteLine("String s2 {0}.", Test(s2));
    Console.WriteLine("String s3 {0}.", Test(s3));
    }    public static String Test(string s)
    {    if (String.IsNullOrEmpty(s)) 
        return "is null or empty";    else
        return String.Format("(\"{0}\") is neither null nor empty", s);
    }
}
时间: 2024-08-08 07:24:00

C# 字符串判空(isNullOrEmpty)的相关文章

StringUtils中 isNotEmpty 和isNotBlank的区别【java字符串判空】

1 isNotEmpty(str)等价于 str != null && str.length > 0 2 isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length > 0 3 同理 4 isEmpty 等价于 str == null || str.length == 0 5 isBlank 等价于 str == null || str.length == 0

Python 2.7.9 - 005.字符串判空

#coding=utf-8 #!/usr/bin/python str1 = None; str2 = ''; str3 = ' '; if str1 == None : print("str1 is none."); else : print("str1 is not none."); if str2 == None : print("str2 is none."); else : print("str2 is not none.&q

JavaScript 字符串判空

下载jQuery $.trim(string) == '' var testVar;console.log($.trim(testVar) == '');var testVar3 = ' ';console.log($.trim(testVar3) == '');var testVar4 = null;console.log($.trim(testVar4) == '');均为true

字符串判空

主要用到两个命令 -n  -z if [ -n "$PID" ]; then echo "PID is not empty" fi if[ -z "$PID" ]; then echo "PID is empty" fi

【转】三种常用的字符串判空串方法

1. 三种常用的字符串判空串方法:Length法:bool isEmpty = (str.Length == 0);Empty法:bool isEmpty = (str == String.Empty);General法:bool isEmpty = (str == ""); 2. 深入内部机制:要探讨这三种方法的内部机制,我们得首先看看.NET是怎样实现的,也就是要看看.NET的源代码!然而,我们哪里找这些源代码呢?我们同样有三种方法:Rotor法:一个不错的选择就是微软的Rotor

String判空效率比较

今天逛社区时忽然看到以下博主时的博文,说字符串判空的效率,觉得口说无凭,于是自己手动测试了一下,以下是我的测试代码,不足之处,还望大神指教 http://blog.csdn.net/fengxuezhiye/article/details/7763384 1.下面是测试100万次的代码 1 package testData; 2 3 public class TestData { 4 public static void main(String[] args) { 5 //不需要导入包 6 //

C# 判断字符串为空的4种方法及效率

在程序开发过程中,少不了要处理字符串,并且常常要判断字符串是否为空,通常有哪些判断方法,以及不同方法的效率又怎么样? 在 C# 中,通常有三种判断字符串是否为空的方法,下面分别探讨. 1.str.Length == 0 使用 str.Length == 0,在三种方法中效率是最高的,但容易产生异常.当字符串为空的时候就会产生异常,如 string str; 或者 string str = null; if(str.Length == 0) //产生异常 此时,就会产生对象不能为空的异常. 如果事

点击button触发onclick事件判空后依旧自动跳转

这是一个前端的问题. 其中判断字符串为空的脚本代码是这样的: var remark = $("#Remark").val(); //判空.注意:var reg = /空格/g; var reg = / /g; var tmp = remark.replace(reg, ""); 先把所有空格去除,通过判断tmp是否为空来判断文本框是否为空. 问题是这样的:我点击button后,触发js脚本判断input框是否为空,如果为空就提示出错.否则提交表单. 结果是这样的:我

String类型判空、

针对前后含有空格.有空格的.空字符串.以及null字符的判断 public static void main(String[] args) { String s1 = ""; String s2 = null; String s3 = " "; String s4 = "hello"; String s5 = " hello "; System.out.println("1:" + isEmpty(s1))