Author Posts

April 26, 2017 at 9:38 am

hopefully a quick one, how do i check if the String $from contains the character * rather than it searching for everything ?

ideally i want the result from the below to produce the ELSE output


$From = "ikea"
$subject = "are*"


if ($from -like "*")

{

write-output "String contains the * character"


}

else {

Write-Output "String does not contain the * Character"

}

Then alternately produce the first match write-output "String contains the * character" from below


$From = "ikea*"
$subject = "are*"


if ($from -like "*")

{

write-output "String contains the * character"


}

else {

Write-Output "String does not contain the * Character"

}

April 26, 2017 at 10:31 am

Hi Mark,

You can use the -match operator if you escape the * with a backslash or the Contains() method of the String object.

Example:

$From = 'ikea'
$subject = 'are*'


if ($from -match '\*')
{
    write-output '-match     : String contains the * character'
}
else {
    Write-Output '-match     : String does not contain the * Character'
}

if ($from.Contains('*'))
{
    write-output '.Contains(): String contains the * character'
}
else {
    Write-Output '.Contains(): String does not contain the * Character'
}

I hope above helps.

Regards,
Daniel

April 26, 2017 at 10:31 am

You can use the match operator to match the string on a regular expression:

$From = "ikea*"
$subject = "are*"

if ($from -match "\*")
{
  write-output "String contains the * character"
}
else 
{
  Write-Output "String does not contain the * Character"
}