Discussion:
How to test if string contains one of multiple strings
(too old to reply)
PaulChavez
2009-06-30 21:09:01 UTC
Permalink
You can use the Contains() method on a string.

$s = "New York City"
"New","Old" | %{ if ($s.contains($_)) { "$s contains $_" }}
How can I test if a string contains one of several possible strings.
if ($string -match "foo") { Do this; Do That }
I want to know $string contains either "foo" or "bar".
Thanks in advance.
Chris Dent
2009-06-30 21:13:09 UTC
Permalink
-match uses regular expressions, so you could use:

if ($string -match "foo|bar") { Do this; Do That }

Chris
How can I test if a string contains one of several possible strings.
if ($string -match "foo") { Do this; Do That }
I want to know $string contains either "foo" or "bar".
Thanks in advance.
C S S
2009-06-30 21:47:01 UTC
Permalink
Post by Chris Dent
if ($string -match "foo|bar") { Do this; Do That }
Chris
Ah, that works great, thank you.

Loading...