1

There are several common conditional statements that are often used, for example:

Whether it is contain, whether it starts with the letters, whether it ends with the letters, whether it is empty.

Can someone write the code in a unified way?

$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"
}
10
  • 1
    take a look at string methods - .StartsWith(), EndsWith(), and .Contains(). if you want a bit more speed, look into regex with -match '^asdfg', -match 'asdfg$', and -match 'asdfg'. for the not empty, use -not [string]IsNullOrEmpty().
    – Lee_Dailey
    Jun 15, 2019 at 1:24
  • Thank you, I have no way to mark your answer, oha
    – tianyi
    Jun 15, 2019 at 1:53
  • $var -eq $null use this to judge whether it is empty?
    – tianyi
    Jun 15, 2019 at 1:59
  • you are most welcome! no need to mark anything as an answer ... i doubt this basic stuff qualifies for an official answer status. [grin]
    – Lee_Dailey
    Jun 15, 2019 at 2:29
  • 2
    @Lee_Dailey you should post it as an answer; it most certainly qualifies. Also slight typo: [string]::IsNullOrEmpty(), but do note that method will also return true if the string is $null, not just "empty". A simpler way to compare for both might be to just let the string coalesce to bool, as in if ($string) or if (-not $string). For a true empty test, maybe comparing to [string]::Empty is better, or just if ($string.Length), with the caveat that some strings might have length 0 without being empty (untested).
    – briantist
    Jun 15, 2019 at 3:32

3 Answers 3

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"
}

Please note: The string (say .Net) methods are case sensitive by default, where PowerShell is case insensitive.

1
  • You can use -cnotmatch for case sensitive.
    – js2010
    Jun 15, 2019 at 12:23
2

what you want are the string methods OR regex & the -match operator. [grin]

here one way to see the methods ...

'asdf' |
    Get-Member -MemberType Methods

here's one way to see the static methods of the [string] type ...

'asdf' |
    Get-Member -Static

for regex, i recommend regex101.com ... [grin]

here's some demo code for the ideas involved ...

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

truncated output ...

++++++++++++++++++++++++++++++++++++++++++++++++++
--- 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
1
  • @tianyi - you are most welcome! glad to have helped a tad ... [grin]
    – Lee_Dailey
    Jun 15, 2019 at 14:59
1

Here's a switch version of answer 2. (switch also has a -regex option)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.