PowerShell で文字列が null または空かどうかを確認するにはどうすればよいですか? 質問する

PowerShell で文字列が null または空かどうかを確認するにはどうすればよいですか? 質問する

IsNullOrEmptyPowerShell には、文字列が null または空かどうかを確認するための組み込みの -like 関数がありますか?

今のところ見つけられず、組み込みの方法がある場合は、そのための関数を書きたくありません。

ベストアンサー1

皆さんはこれを難しくしすぎています。PowerShell はこれを非常にエレガントに処理します。例:

> $str1 = $null
> if ($str1) { 'not empty' } else { 'empty' }
empty

> $str2 = ''
> if ($str2) { 'not empty' } else { 'empty' }
empty

> $str3 = ' '
> if ($str3) { 'not empty' } else { 'empty' }
not empty

> $str4 = 'asdf'
> if ($str4) { 'not empty' } else { 'empty' }
not empty

> if ($str1 -and $str2) { 'neither empty' } else { 'one or both empty' }
one or both empty

> if ($str3 -and $str4) { 'neither empty' } else { 'one or both empty' }
neither empty

おすすめ記事