• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to test if string contains one of multiple strings

C

C S S

#1
How can I test if a string contains one of several possible strings.
Whereas I can use the contains or match operator like so:

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

I want to know $string contains either "foo" or "bar".

Thanks in advance.
 

My Computer

P

PaulChavez

#2
You can use the Contains() method on a string.

$s = "New York City"
"New","Old" | %{ if ($s.contains($_)) { "$s contains $_" }}



"C S S" wrote:

>
> How can I test if a string contains one of several possible strings.
> Whereas I can use the contains or match operator like so:
>
> if ($string -match "foo") { Do this; Do That }
>
> I want to know $string contains either "foo" or "bar".
>
> Thanks in advance.
>
>
>
 

My Computer