Powershell 表示几种常用的条件语句

Powershell 表示几种常用的条件语句,powershell,powershell-4.0,Powershell,Powershell 4.0,有几种常用的条件语句,例如: 是否包含,是否以字母开头,是否以字母结尾,是否为空 有人能以统一的方式编写代码吗 $var = "word somestring" if ($var --Does not contain-- "somestring") { Write-Host "true" } 请注意:默认情况下,字符串(比如.Net)方法区分大小写,其中PowerShell不区分大小写 请注意:默认情况下,字符串(比如.Net)方法区分大小写,其中PowerShell不区分大小写。您需

有几种常用的条件语句,例如:

是否包含,是否以字母开头,是否以字母结尾,是否为空

有人能以统一的方式编写代码吗

$var = "word somestring"
if ($var --Does not contain-- "somestring") {
    Write-Host "true"
}
请注意:默认情况下,字符串(比如.Net)方法区分大小写,其中PowerShell不区分大小写


请注意:默认情况下,字符串(比如.Net)方法区分大小写,其中PowerShell不区分大小写。

您需要的是字符串方法或regex&匹配运算符。[咧嘴笑]

这里有一种方法可以查看这些方法

'asdf' |
    Get-Member -MemberType Methods
这里有一种方法可以查看
[string]
类型的静态方法

'asdf' |
    Get-Member -Static
对于regex,我推荐regex101.com。。。[咧嘴笑]

下面是一些演示代码,用于演示所涉及的想法

$StringList = @(
    'qwerty is at the start of this string'
    'at the string-end, is qwerty'
    'in the middle, qwerty is placed'
    'the target word is not in this string'
    # below is an empty string
    ''
    )
$Target = 'qwerty'

foreach ($SL_Item in $StringList)
    {
    '+' * 50
    '--- Current test string   = "{0}"' -f $SL_Item
    '=== String Methods ==='
    'Target at start           = {0}' -f $SL_Item.StartsWith($Target)
    'Target at end             = {0}' -f $SL_Item.EndsWith($Target)
    'Target anywhere in string = {0}' -f $SL_Item.Contains($Target)
    ''
    '=== Regex ==='
    'Target at start           = {0}' -f ($SL_Item -match "^$Target")
    'Target at end             = {0}' -f ($SL_Item -match "$Target$")
    'Target anywhere in string = {0}' -f ($SL_Item -match $Target)
    ''
    '=== Empty ==='
    'String is $Null or empty  = {0}' -f [string]::IsNullOrEmpty($SL_Item)
    'String is $Null or empty  = {0}' -f ([string]::Empty -eq $SL_Item)
    ''
    }
截断的输出

++++++++++++++++++++++++++++++++++++++++++++++++++
--- Current test string   = "qwerty is at the start of this string"
=== String Methods ===
Target at start           = True
Target at end             = False
Target anywhere in string = True

=== Regex ===
Target at start           = True
Target at end             = False
Target anywhere in string = True

=== Empty ===
String is $Null or empty  = False
String is $Null or empty  = False

[*...snip...*] 

++++++++++++++++++++++++++++++++++++++++++++++++++
--- Current test string   = ""
=== String Methods ===
Target at start           = False
Target at end             = False
Target anywhere in string = False

=== Regex ===
Target at start           = False
Target at end             = False
Target anywhere in string = False

=== Empty ===
String is $Null or empty  = True
String is $Null or empty  = True

您需要的是字符串方法或regex&匹配运算符。[咧嘴笑]

这里有一种方法可以查看这些方法

'asdf' |
    Get-Member -MemberType Methods
这里有一种方法可以查看
[string]
类型的静态方法

'asdf' |
    Get-Member -Static
对于regex,我推荐regex101.com。。。[咧嘴笑]

下面是一些演示代码,用于演示所涉及的想法

$StringList = @(
    'qwerty is at the start of this string'
    'at the string-end, is qwerty'
    'in the middle, qwerty is placed'
    'the target word is not in this string'
    # below is an empty string
    ''
    )
$Target = 'qwerty'

foreach ($SL_Item in $StringList)
    {
    '+' * 50
    '--- Current test string   = "{0}"' -f $SL_Item
    '=== String Methods ==='
    'Target at start           = {0}' -f $SL_Item.StartsWith($Target)
    'Target at end             = {0}' -f $SL_Item.EndsWith($Target)
    'Target anywhere in string = {0}' -f $SL_Item.Contains($Target)
    ''
    '=== Regex ==='
    'Target at start           = {0}' -f ($SL_Item -match "^$Target")
    'Target at end             = {0}' -f ($SL_Item -match "$Target$")
    'Target anywhere in string = {0}' -f ($SL_Item -match $Target)
    ''
    '=== Empty ==='
    'String is $Null or empty  = {0}' -f [string]::IsNullOrEmpty($SL_Item)
    'String is $Null or empty  = {0}' -f ([string]::Empty -eq $SL_Item)
    ''
    }
截断的输出

++++++++++++++++++++++++++++++++++++++++++++++++++
--- Current test string   = "qwerty is at the start of this string"
=== String Methods ===
Target at start           = True
Target at end             = False
Target anywhere in string = True

=== Regex ===
Target at start           = True
Target at end             = False
Target anywhere in string = True

=== Empty ===
String is $Null or empty  = False
String is $Null or empty  = False

[*...snip...*] 

++++++++++++++++++++++++++++++++++++++++++++++++++
--- Current test string   = ""
=== String Methods ===
Target at start           = False
Target at end             = False
Target anywhere in string = False

=== Regex ===
Target at start           = False
Target at end             = False
Target anywhere in string = False

=== Empty ===
String is $Null or empty  = True
String is $Null or empty  = True

这是答案2的切换版本。(开关还具有-regex选项)


这是答案2的切换版本。(开关还具有-regex选项)


看看字符串方法-
.StartsWith()
EndsWith()
,和
.Contains()
。如果您想加快一点速度,请查看带有
-match'^asdfg'
-match'asdfg$'
-match'asdfg'
的正则表达式。对于
非空的
,请使用
-not[string]IsNullOrEmpty()
。谢谢,我无法标记您的答案,oha$var-eq$null使用此选项判断它是否为空?非常欢迎!不需要将任何东西标记为答案。。。我怀疑这些基本的东西是否符合官方答复的资格。[露齿而笑]@Lee_Dailey你应该把它作为一个答案发布;它完全符合条件。还有轻微的输入错误:
[string]::IsNullOrEmpty()
,但请注意,如果字符串为
$null
,而不仅仅是“空”,则该方法也将返回true。比较两者的一种更简单的方法可能是让字符串合并到bool,如
if($string)
if(-not$string)
。对于真正的空测试,可能与
[string]相比::empty
更好,或者如果($string.Length),则只需
,但需要注意的是(未测试)。请看一下字符串方法-
.StartsWith()
EndsWith()
,以及
Contains()
。如果您想加快一点速度,请查看带有
-match'^asdfg'
-match'asdfg$'
-match'asdfg'
的正则表达式。对于
非空的
,请使用
-not[string]IsNullOrEmpty()
。谢谢,我无法标记您的答案,oha$var-eq$null使用此选项判断它是否为空?非常欢迎!不需要将任何东西标记为答案。。。我怀疑这些基本的东西是否符合官方答复的资格。[露齿而笑]@Lee_Dailey你应该把它作为一个答案发布;它完全符合条件。还有轻微的输入错误:
[string]::IsNullOrEmpty()
,但请注意,如果字符串为
$null
,而不仅仅是“空”,则该方法也将返回true。比较两者的一种更简单的方法可能是让字符串合并到bool,如
if($string)
if(-not$string)
。对于真正的空测试,可能与
[string]:empty
比较好,或者如果($string.Length)
,只需
,但需要注意的是(未测试)。您可以使用-cnotmatch区分大小写。您可以使用-cnotmatch区分大小写。@tianyi-非常欢迎您!很高兴能帮上一点忙。。。[咧嘴笑]@tianyi-不客气!很高兴能帮上一点忙。。。[咧嘴笑]
$StringList = 'qwerty is at the start of this string', 
  'at the string-end, is qwerty', 'in the middle, qwerty is placed', 
  'the target word is not in this string', ''

$Target = 'qwerty'

switch -wildcard ($StringList) {
  { $_.StartsWith($Target)      } { "'$target' at start of '$_'" }
  { $_.EndsWith($Target)        } { "'$target' at end of '$_'" }
  { $_.Contains($Target)        } { "'$target' anywhere in '$_'" }
  { $_ -match "^$Target"        } { "'$target' at start of '$_' (regex)" }
  { $_ -match "$Target$"        } { "'$target' at end of '$_' (regex)" }
  { $_ -match $Target           } { "'$target' anywhere in '$_' (regex)" }
  { [string]::IsNullOrEmpty($_) } { "'$target' not in IsNullOrEmpty() '$_'" }
  { ([string]::Empty -eq $_)    } { "'$target' not in ::Empty '$_'" }
  $target*                        { "'$target' start of '$_' (wildcard)" }
  *$target                        { "'$target' end of '$_' (wildcard)" }
  *$target*                       { "'$target' anywhere in '$_' (wildcard)" }
  ''                              { "'$target' not in empty '$_'" }
}


'qwerty' is at start of 'qwerty is at the start of this string'
'qwerty' is anywhere in 'qwerty is at the start of this string'
'qwerty' at start of 'qwerty is at the start of this string' (regex)
'qwerty' anywhere in 'qwerty is at the start of this string' (regex)
'qwerty' start of 'qwerty is at the start of this string' (wildcard)
'qwerty' anywhere in 'qwerty is at the start of this string' (wildcard)
'qwerty' is at end of 'at the string-end, is qwerty'
'qwerty' is anywhere in 'at the string-end, is qwerty'
'qwerty' is at end of 'at the string-end, is qwerty' (regex)
'qwerty' anywhere in 'at the string-end, is qwerty' (regex)
'qwerty' end of 'at the string-end, is qwerty' (wildcard)
'qwerty' anywhere in 'at the string-end, is qwerty' (wildcard)
'qwerty' is anywhere in 'in the middle, qwerty is placed'
'qwerty' anywhere in 'in the middle, qwerty is placed' (regex)
'qwerty' anywhere in 'in the middle, qwerty is placed' (wildcard)
'qwerty' not in IsNullOrEmpty() ''
'qwerty' not in ::Empty ''
'qwerty' not in empty ''