表达几种常用的条件语句
Express several commonly used conditional statements
有几种常用的条件语句,例如:
是否包含,是否以字母开头,是否以字母结尾,是否为空
谁能统一写代码?
$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"
}
$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 不区分大小写。
您想要的是字符串方法或正则表达式和 -match
运算符。 [咧嘴一笑]
这里是查看方法的一种方式...
'asdf' |
Get-Member -MemberType Methods
这是查看 [string]
类型的静态方法的一种方法...
'asdf' |
Get-Member -Static
对于正则表达式,我推荐 regex101.com ... [grin]
这里有一些演示代码用于所涉及的想法......
$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 选项)
$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 ''
有几种常用的条件语句,例如:
是否包含,是否以字母开头,是否以字母结尾,是否为空
谁能统一写代码?
$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"
}
$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 不区分大小写。
您想要的是字符串方法或正则表达式和 -match
运算符。 [咧嘴一笑]
这里是查看方法的一种方式...
'asdf' |
Get-Member -MemberType Methods
这是查看 [string]
类型的静态方法的一种方法...
'asdf' |
Get-Member -Static
对于正则表达式,我推荐 regex101.com ... [grin]
这里有一些演示代码用于所涉及的想法......
$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 选项)
$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 ''