How to test if string contains one of multiple strings


  1.    30 Jun 2009 #1
    C S S Guest

    How to test if string contains one of multiple strings



    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 System SpecsSystem Spec

  2.    30 Jun 2009 #2
    PaulChavez Guest

    RE: How to test if string contains one of multiple strings


    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 System SpecsSystem Spec

  3.    30 Jun 2009 #3
    Join Date : Jun 2009
    Posts : 10
    XP
    Local Time: 04:12


     

    Re: How to test if string contains one of multiple strings


    There is a bug with this part of powershell which makes this totally unusable.
    It will only work if the strings do not contain certain chars...

    See post
    Horrible File Name Gobble Wildcard -cmatch -match BUG
      My System SpecsSystem Spec

  4.    30 Jun 2009 #4
    Join Date : Jun 2009
    Posts : 10
    XP
    Local Time: 04:12


     

    Re: How to test if string contains one of multiple strings


    if ( ($string1.Contains($stringA)) -or ($string1.Contains($stringB)) )
    {
    hack the gibson
    }

    should work tho
      My System SpecsSystem Spec

How to test if string contains one of multiple strings

Similar Threads
Thread Forum
Multiple string -notmatch
I'm trying to list all my disabled users and filter out 2 (Guest and KRBTGT) I've tried get-qaduser -disable | ? { @("Guest","krbtgt") -notmatch...
PowerShell
find and replace a specific string in multiple files
basically, i have a vbscript which does a find and replace on single file, below i've shared the script code. ' replace.vbs Find Replacewith File...
VB Script
Newbie question: replace multiple strings in multiple text files
Hi there -- I've never created a script before now, and what I want to do is periodically have a script go into fifteen text files and do...
VB Script
Using test-path on multiple remote machiens.
after I query AD for all computers I want to then verify if a path exists on each machine in AD. Below is the script as I have created. $root =...
PowerShell
Replacing Multiple Characters In A String
I created a powershell script to create directories and rename files based on MP3 tags.. MP3 tags occasionally have characters the can't be used...
PowerShell
Fast way to determine if a string contains a member of a list of strings
I need to find the fastest way in terms of storage and searching to determine if a given string contains one of a member of a list of strings. So,...
.NET General
convert array of strings to string
Dumb question time... I've got an array of strings, e.g. 1# $a = get-content myfile.txt 2# $a line 1 line 2 line 3 My desired result is...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 03:12.
    .