Home > other >  compare string with double quote
compare string with double quote

Time:12-02

I want to compare a string with double quotes. I tried the following code but didn't work.

The sample $inputStr is '{"to":"[Email]","from":"[email protected]","cc":"","bc":""'

$inputStr = '{"to":"[Email]","from":"[email protected]","cc":"","bc":""'
$emailStr = '"[Email]"'
if ($inputStr -like "*" $emailStr "*") {
   write-host "exists"
}

Any suggestions?

Thanks

CodePudding user response:

The comparison didn't work because [ and ] are wildcard characters which need to be escaped either using a backtick ` ('"`[Email`]"') or alternatively, with WildcardPattern.Escape method:

$inputStr = '{"to":"[Email]","from":"[email protected]","cc":"","bc":""'
$emailStr = '"[Email]"'
if ($inputStr -like "*$([WildcardPattern]::Escape($emailStr))*") {
   Write-Host "exists"
}
  • Related