Check if a string is NULL or EMPTY using PowerShell

http://techibee.com/powershell/check-if-a-string-is-null-or-empty-using-powershell/1889

Check if a string is NULL or EMPTY using PowerShell
by TECHIBEE on OCTOBER 10, 2012

In this post, I will show you how to verify if a string is empty, null or having white spaces using Powershell.

Checking if a string is NULL or EMPTY is very common requirement in Powershell script. If we don’t do that we will end up with run time errors if we try to perform some operation on that string variable which is empty or null. So the question now is, how to check it?

Well, below is the most often used technique to check if a string is NULL or empty

if($mystring) {
Write-Host "string is not empty"
} else {
Write-Host "String is EMPTY or NULL"
}
Most scripts use this method, however we can make things better by using “System.String” dotnet class. It has a method IsNullOrEmpty() which returns true if the passed string is null or empty. See the below example for clarity

IF([string]::IsNullOrEmpty($mystring)) {
Write-Host "Given string is NULL or EMPTY"
} else {
Write-Host "Given string has a value"
}
Both the methods described above gives you the same functionality. But if you want more meaningful and neat look to your code, I would prefer the second method.

Now you might get a question, what if the $mystring has white space as value. It is neither a EMPTY value nor a NULL value so we can not use IsNullOrEmpty() method in this case. If you try it, it will return False which means string is not NULL or EMPTY. In such cases we can use another method available with System.String class. That is IsNullOrWhiteSpace() . The good thing about this method is it covers all three cases, NULL, EMPTY, and WHITESPACE. It will work for any number whitespaces in the variable. See the below examples for clarity

IF([string]::IsNullOrWhiteSpace($string1)) {
Write-Host "Given string is NULL or having WHITESPACE"
} else {
Write-Host "Given string has a value"
}

$string1 = " "
IF([string]::IsNullOrWhiteSpace($string1)) {
Write-Host "Given string is NULL or having WHITESPACE"
} else {
Write-Host "Given string has a value"
}

$string1 = ""
IF([string]::IsNullOrWhiteSpace($string1)) {
Write-Host "Given string is NULL or having WHITESPACE"
} else {
Write-Host "Given string has a value"
}
After executing above code you will get Given string is NULL or having WHITESPACE three times in output.  So, it is clear that theIsNullOrWhiteSpace()method is working for detecting NULL, EMPTY and WHILESPACE strings.

Hope this helps and happy learning..

Please feel free to write in comments section if you have any questions

时间: 2024-10-05 05:56:56

Check if a string is NULL or EMPTY using PowerShell的相关文章

[shiro] Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.

访问某页面时,出现了这个异常: java.lang.IllegalArgumentException: Wildcard string cannot be null or empty. Make sure permission strings are properly formatted. at org.apache.shiro.authz.permission.WildcardPermission.setParts(WildcardPermission.java:154) at org.apa

Difference between null and empty String

String s1 = ""; means that the empty String is assigned to s1. In this case, s1.length() is the same as "".length(), witch will yield 0 as expected. String s2 = null; means that (null) or "no value at all" is assigned to s2.

【转载】String.Empty、string=”” 和null的区别

String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: ? 1 2 3 4 5 6 7 8 9 10 11 12 public static readonly string Empty; //这就是String.Empty 那是只读的String类的成员,也是string的变量的默认值是什么呢? //String的构造函数 static String(){     Empty = "";

String.Empty、string=”” 和null的区别

String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: 1 2 3 4 5 6 7 8 9 10 11 12 public static readonly string Empty; //这就是String.Empty 那是只读的String类的成员,也是string的变量的默认值是什么呢? //String的构造函数 static String(){     Empty = "";//

转:String.Empty、string=”” 和null的区别

原文地址:http://www.cnblogs.com/fanyong/archive/2012/11/01/2750163.html String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: public static readonly string Empty; //这就是String.Empty 那是只读的String类的成员,也是string的变量的默认值是什么呢? //String的构造函

(转)Java 中关于String的空对象(null) ,空值(empty),空格

原文出处:Java 中关于String的空对象(null) ,空值(empty),空格 定义 空对象: String s = null; 空对象是指定义一个对象s,但是没有给该对象分配空间,即没有实例化该对象,因此,空对象在调用所有对象方法时候都会抛出异常,如s.length(), s.isEmpty()等方法. 空值: String k = ""; 空值是指一个字符床对象已经实例化,即系统已经给该变量分配了空间,只是对象的内容为空. 空格: String n = " &qu

PHP中空字符串介绍0、null、empty和false之间的关系

0是数字,是empty,是false,不是null,值相当于空字符串,但类型不是字符串,去空格或强制转换为字符串型时不等于空字符串 ""的值相当于0,是empty,是空字符串,是false,不是null,不是数字字符串 null的值相当于空字符串,值相当于0,是empty,是false,不是字符串,不是数字0 "=="只要值相等就满足条件: "==="需要两个变量的值和类型都相等: strval();将变量转换为字符串类型: intval();将

string s = null 和 string s = “”的区别

string s = null; 表示一个空串,没有占用了空间,不在内存中开辟空间 string s = "";在内存中开辟空间,但空间中没有值(""也是一个字符串)表示一个空串,被实列化了,占用了内存空间 null 表示一个空引用,"" 表示一个空字符串, string.Empty和“”类似,在内存中分配0个字节 如果想声明一个初始值为空的字符串变量最好用 string str = string.Empty;

String.valueOf(null) 报空指针

String.valueOf 默认的方法 argument 可以为null 的 boolean b = null; char c = null; char[] data = null; double d = null; float f = null; int i = null; long l = null; Object obj = null; String.valueOf(null) 会调用更为具体valueOf(char[] data) /** * Returns the string re