locked
if string contains more than once RRS feed

  • Question

  • Hi Scripting Guys!

    Hopefully a quick one for you all here. I've gone blank and can't think of the best way to do this; when I say best way I really mean Can't think how to do this.

    I have a fairly large set of text and I want to find out if a certain word appears more than once . Lets say the word is "Reject"

    This is my statement..

    foreach ($B in $A) {
    [string]$C = $B.HTMLBody
    if ($C -like "*Reject*") {
    Write-Host "True"
     }
     else {
    Write-Host "not true"
    }
    }

    How would I add to this to find out if reject appears more than once?

    Thanks in advance

    Joe

    Wednesday, June 10, 2015 7:22 PM

Answers

  • Use IndexOf

    When you get the position of the first one use substring to start a search in the substring for the next.

    Or use IndexOf and LastIndexOf and compare.  If the are the same then there is only one.  If they are different then there is more thanone.


    \_(ツ)_/

    • Marked as answer by JOEs_SG Wednesday, June 10, 2015 7:48 PM
    Wednesday, June 10, 2015 7:28 PM

All replies

  • Use IndexOf

    When you get the position of the first one use substring to start a search in the substring for the next.

    Or use IndexOf and LastIndexOf and compare.  If the are the same then there is only one.  If they are different then there is more thanone.


    \_(ツ)_/

    • Marked as answer by JOEs_SG Wednesday, June 10, 2015 7:48 PM
    Wednesday, June 10, 2015 7:28 PM
  • Haven't tested this, but you could use a variation of .Replace() to change the first instance of the string and then do the test a second time to see if it's still found. Replace itself will replace all occurrences of the string, but as described here https://social.technet.microsoft.com/Forums/scriptcenter/en-US/63ace2a7-91f0-447d-ba33-bb526518564e/powershell-regular-expressions-replace-operator-disable-the-default-global-option?forum=ITCG you can do it via Regex. So you'd end up with something like :

    foreach ($B in $A) {
    [string]$C = $B.HTMLBody
    if ($C -like "*Reject*") {
       Write-Host "1 Found"
       $str = $C
       $regex = [Regex] "Reject"
       $D = $regex.Replace($s, "XXXXX", 1)
       if ($D -like "*Reject*") {
          Write-Host "2 Found"
          }
       }
    else {
       Write-Host "not true"
       }
    }
    Again, I haven't tested but I think it should work.
    Wednesday, June 10, 2015 7:46 PM
  • Thanks Jrv, that did the trick. Took a couple of attempts, seems to be case sensitive, is that right?
    Wednesday, June 10, 2015 7:49 PM
  • Thanks Keith, I've got Jrv's method working so i'll use that for now. I'll come back and try yours in the morning and see if there's any benefits.

    Joe

    Wednesday, June 10, 2015 7:51 PM
  • Cool, either way glad to hear you got a solution. Would certainly be interested to know if it does actually work.
    Wednesday, June 10, 2015 7:53 PM
  • ('ccchellocc').IndexOf('HELLO',[System.StringComparison]::InvariantCultureIgnoreCase)


    \_(ツ)_/


    • Edited by jrv Wednesday, June 10, 2015 8:20 PM
    Wednesday, June 10, 2015 8:18 PM
  • This is even easier:

    PS C:\scripts> $s='this is a this can be a this'
    PS C:\scripts> ($s.Split(' ')|?{$_ -eq 'this'}).count
    3
    PS C:\scripts>


    \_(ツ)_/

    Wednesday, June 10, 2015 8:24 PM