表达几个常用的条件语句

时间:2019-06-15 00:44:35

标签: powershell powershell-4.0

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

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

有人可以用统一的方式编写代码吗?

$var = "word somestring"
if ($var --Does not contain-- "somestring") {
    Write-Host "true"
}
$var="word somestring"
if ($var --Does not start with-- "somestring") {
    Write-Host "true"
}
$var = "word somestring"
if ($var --Does not Ends with-- "somestring") {
    Write-Host "true"
}
$var = "word somestring"
if ($var --Is Not Empty--) {
    Write-Host "true"
}

3 个答案:

答案 0 :(得分:2)

$var = "word somestring"

# $var --Does not contain-- "somestring"
if ($var -notmatch "somestring") {
    Write-Host "true"
}

# $var --Does not start with-- "somestring"
if ($var -notmatch "^somestring") {
    Write-Host "true"
}

# $var --Does not start with-- "somestring"  - case sensitive
if (-not $var.StartsWith("somestring")) {
    Write-Host "true"
}

# $var --Does not Ends with-- "somestring"
if ($var -notmatch "somestring`$") {
    Write-Host "true"
}

# $var --Does not Ends with-- "somestring"  - case sensitive
if (-not $var.EndsWith("somestring")) {
    Write-Host "true"
}

# $var --Is Not Empty--
if (-not [String]::IsNullOrEmpty($var)) {
    Write-Host "true"
}

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

答案 1 :(得分:2)

您想要的是字符串方法OR regex和-match运算符。 [咧嘴]

这里是查看方法的一种方法...

'asdf' |
    Get-Member -MemberType Methods

这是查看[string]类型的静态方法的一种方法...

'asdf' |
    Get-Member -Static

对于正则表达式,我建议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 :(得分:1)

这是答案2的转换版本。(转换也具有-regex选项)

$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 ''