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
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 su ...
- 转:String.Empty、string=”” 和null的区别
原文地址:http://www.cnblogs.com/fanyong/archive/2012/11/01/2750163.html String.Empty是string类的一个静态常量: Str ...
- String.Empty、string=”” 和null的区别
String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: 1 2 3 4 5 6 7 8 9 10 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 ...
- [转载]String.Empty、string=”” 和null的区别
String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: 1 2 3 4 5 6 7 8 9 10 1 ...
- Summary: Difference between null and empty String
String s1 = ""; means that the empty String is assigned to s1. In this case, s1.length() i ...
- (转)Java 中关于String的空对象(null) ,空值(empty),空格
原文出处:Java 中关于String的空对象(null) ,空值(empty),空格 定义 空对象: String s = null; 空对象是指定义一个对象s,但是没有给该对象分配空间,即没有实例 ...
- PHP中空字符串介绍0、null、empty和false之间的关系
PHP中空字符串介绍0.null.empty和false之间的关系 作者: 字体:[增加 减小] 类型:转载 时间:2012-09-25 用PHP开发那么久,PHP中空字符串.0.null.emp ...
- String s ; 和 String s = null ; 和 String s = "" ; 的却别
String s ;该语句表示只是声明了一个引用变量,但是并没有初始化引用,所以对变量s的任何操作(除了初始化赋值外) 都将引发异常. String s=null; 表示未申请任何内存资源,即此语句表 ...
随机推荐
- Jquery 的bind(), live(), delegate(), on()绑定事件方式
1. 因为项目中经常会有利用jquery操作dom元素的增删操作,所以会涉及到dom元素的绑定事件方式,简单的归纳一下bind,live,delegate,on的区别,以便以后查阅. bind() 适 ...
- DORIS-软件网址
DORIS下载网址:http://doris.tudelft.nl/ Doris软件最新的信息总可以在这个网址上查到:http://enterprise.tudelft.nl/doris/
- [AngularJS] Design Pattern: Simple Mediator
We're going to use rootScope emit here to send out events and then we're going to listen for them in ...
- Zend框架2入门(一) (转)
By Rob Allen, www.akrabat.com 修订0.1.2文件版权所有? 2011本教程的目的是给创建一个简单的数据库的介绍使用Zend Framework 2驱动的应用程序使用模型 ...
- unity3d Find()使用
1. Hierarchy 创建对象如两个cube时,未修改名称,名称都为cube时. js添加至Camera: private var cubeObj : GameObject; //private ...
- iTerm2和oh-my-zsh的个性化定制
终于在某东做活动新入手了一台air,看身边小伙伴的mac的终端感觉好炫酷,于是乎准备自己也捯饬捯饬,google了一下,发现了osx平台上的终端神器iTerm2和用来代替bash的oh-my-zsh, ...
- 常用PC服务器LSI阵列卡配置
通常,我们使用的DELL/HP/IBM三家的机架式PC级服务器阵列卡是从LSI的卡OEM出来的,DELL和IBM两家的阵列卡原生程度较高,没有做太多封装,可以用原厂提供的阵列卡管理工具进行监控:而HP ...
- Tomcat相关目录及配置文件总结
Tomcat根目录介绍 [bin]目录主要是用来存放tomcat的命令,主要有两大类,一类是以.sh结尾的(linux命令),另一类是以.bat结尾的(windows命令). 很多环境变量的 ...
- css基础回顾-定位:position
w3school 对position定义和说明是: 定义和用法: position 属性规定元素的定位类型. 说明: 这个属性定义建立元素布局所用的定位机制.任何元素都可以定位,不过绝对或固定元素会生 ...
- asp.net微信开发第三篇----自定义会话管理
和微信用户的沟通少不了,总觉得看起来微信官网后台管理中的会话回复消息有点呆板,所以我这里就自定义了一个会话管理功能,最终效果图如下: 因为我试使用富文本文件CKEDITOR来进行编写,你看到稳中可能会 ...