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. ssh端口映射,本地转发

    应用场景: # HOSTA<-X->HOSTB 表示A,B两机器相互不可以访问,  HOSTA<-->HOSTB 表示A,B两机器可以相互访问# 1.localhost< ...

  2. SKAction类

    继承自 NSObject 符合 NSCodingNSCopyingNSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit.framewo ...

  3. Android SimpleAdapter ListView (锁定手机,解锁手机的列表)

    SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局. 构造方法: SimpleAdapter(Context context, List<? extends Map< ...

  4. WebView中的视频全屏的相关操作

    近期工作中,基本一直在用WebView,今天就把它整理下: WebView 顾名思义,就是放一个网页,一个看起来十分简单,可是用起来不是那么简单的控件. 首先你肯定要定义,初始化一个webview,事 ...

  5. Android app内语言环境切换

    逻辑很简单: 1  app内所有activity继承自BaseActivity或BaseActivity派生出来的子类,BaseActivity中维护了一个静态的 app Activity访问栈,在创 ...

  6. table标签

    table标签使我们最常用的的标签,在使用table标签时我们要注意一些其属性,早期我们经常使用table标签对其进行页面布局但是现在我们基本不再使用,由此可见table标签也是非常强大的一个工具. ...

  7. [转] linux 信号量之SIGNAL

    我们可以使用kill -l查看所有的信号量解释,但是没有看到SIGNAL 0的解释. [root@testdb~]# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) ...

  8. centos5.5上apache快速安装H264流媒体支持MP4-H264边下边播

    2013年的某一天,客户反馈北京同事做的广告视频下载速度好慢,几MB的视频在手机上要下载接近一分钟才能开始播放. 我分析后发现两点:1)托管的服务器没支持流媒体:2)广告视频MP4并非流媒体格式. 对 ...

  9. Java分页类 Page

    import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Iterator; ...

  10. java中保留几位小数

    public class NumUtils { /** * 保留两位小数 * * @param d * @return */ public static String get2Wei(double d ...