博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Check if a string is NULL or EMPTY using PowerShell
阅读量:5318 次
发布时间:2019-06-14

本文共 2654 字,大约阅读时间需要 8 分钟。

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

转载于:https://www.cnblogs.com/micro-chen/p/5941653.html

你可能感兴趣的文章
FILTER:progid:DXImageTransform.Microsoft.Gradient使用
查看>>
POJ 3208-Apocalypse Someday(数位dp)
查看>>
Stamps and Envelope Size
查看>>
取某字段不为空的数据is not null
查看>>
当失去焦点时 验证时分秒 并提示
查看>>
增加控制条到视频播放
查看>>
Javascript模块化编程(三):require.js的用法
查看>>
HTML: Dom event
查看>>
titanium好的学习网站推荐
查看>>
Flyweight模式_Java中23种设计模式
查看>>
vue 中生成二维码之爬坑之路
查看>>
@Component 和 @Bean 的区别
查看>>
Linux安全篇-iptables
查看>>
div中溢出文字用点代替
查看>>
UVA - 489 Hangman Judge
查看>>
返回一个一维数组环中的数相加的最大的和
查看>>
55分钟学会正则表达式
查看>>
python1119-20181205作业-郭恩赐提交
查看>>
LA3490 Generator(KMP + 高斯消元)
查看>>
kaldi学习 - 一脚本流学习工具使用
查看>>