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

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

  1. [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 su ...

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

    原文地址:http://www.cnblogs.com/fanyong/archive/2012/11/01/2750163.html String.Empty是string类的一个静态常量: Str ...

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

    String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: 1 2 3 4 5 6 7 8 9 10 1 ...

  4. (后端)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 su ...

  5. [转载]String.Empty、string=”” 和null的区别

    String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: 1 2 3 4 5 6 7 8 9 10 1 ...

  6. Summary: Difference between null and empty String

    String s1 = ""; means that the empty String is assigned to s1. In this case, s1.length() i ...

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

    原文出处:Java 中关于String的空对象(null) ,空值(empty),空格 定义 空对象: String s = null; 空对象是指定义一个对象s,但是没有给该对象分配空间,即没有实例 ...

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

    PHP中空字符串介绍0.null.empty和false之间的关系 作者: 字体:[增加 减小] 类型:转载 时间:2012-09-25   用PHP开发那么久,PHP中空字符串.0.null.emp ...

  9. String s ; 和 String s = null ; 和 String s = "" ; 的却别

    String s ;该语句表示只是声明了一个引用变量,但是并没有初始化引用,所以对变量s的任何操作(除了初始化赋值外) 都将引发异常. String s=null; 表示未申请任何内存资源,即此语句表 ...

随机推荐

  1. int.Parse()与int.TryParse()

      int i = -1;bool b = int.TryParse(null, out i);执行完毕后,b等于false,i等于0,而不是等于-1,切记. int i = -1;bool b = ...

  2. WINDOWS UPDAET

      相信各位或多或少都知道 Windows Update 功能,Windows 有了它就可以随时升级到最新的程序版本,同时对防止安全威胁也能起到很大的作用 在 Windows 7 中,安装完成首次运行 ...

  3. Unity3D NGUI UIPlayTween控件(一)动态打开关闭窗口

    利用NGUI自带的脚本控件实现按钮点击窗口滑动出现与隐藏. 创建界面 首先建立如下图的三个BUtton与三个Panel 绑定脚本 然后在每个Button上添加UIPlayTween脚本,在Intera ...

  4. Collection子接口(List/Set/Queue/SortedSet)

    Collection基本的子接口: List:能够存放反复内容 Set:不能存放反复内容,全部反复的内容靠hashCode()和equals()两个方法区分 Queue:队列接口 SortedSet: ...

  5. android实现界面左右滑动(GridView动态设置item,支持每个item按某个属性排序来显示在不同的界面)

    效果图 :                         分别是第一页.第二页.第三页,随手截的图,不整齐,勿见怪.开始走了弯路,废了不少时间. 思路如下: 1.用ViewPager实现左右分页滑动 ...

  6. SQL报错error:索引中丢失IN或OUT參数

    简单记录下: 今天mybatis中遇到一个错误: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallba ...

  7. 灵活性比Listview更好的RecycleView

    RecycleView:是Android L版本中新添加的一个用来取代ListView的SDK,它的灵活性与可替代性比listview更好. RecyclerView与ListView原理是类似的:都 ...

  8. 检查主机是否存活的shell脚本

    #!/bin/bash PREFIX= num= " ]; do echo -en "Pinging ${PREFIX}.${num}..." >& &qu ...

  9. ASP.NET-FineUI开发实践-9(二)

    其实我也不会,老实教人学怕误人子弟,但是抱着毁人不倦的精神还是糊弄糊弄个别小白吧,最起码能加点原创. 下面以表单为例,打开官方项目,版本为FineUI_4.1.1,打开form_compare页,右键 ...

  10. OD: DEP - Ret2Libc via VirtualProtect() & VirtualAlloc()

    一,通过 VirutalProtect() 修改内存属性绕过 DEP DEP 的四种工作模式中,OptOut 和 AlwaysOn 下所有进程默认都开启 DEP 保护,这里如果一个程序自身需要从堆栈中 ...