Substring to after the nth occurence of a character

Welcome Forums General PowerShell Q&A Substring to after the nth occurence of a character

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
3 years, 8 months ago.

  • Author
    Posts
  • #25012

    Participant
    Points: 1
    Rank: Member

    I'm trying to strip of a portion of a string into a new variable, and am having issues as to how to substring into the starting position I need.

    The actual starting position isn't consistent that I could use that value. The data I want to strip off isn't consistent such that I could count from the end back to where I need to start.

    The only thing I know for certain is there are 3 occurrences of a blank prior to the data I need. After I encounter the 3rd blank, then I want to strip off everything after that.

    I've been playing with this code, but it only strips off the value prior to the first blank(Value1). It does continue to loop through the While statement, but doesn't strip off Value2 or Value3 for me(I want everything following with Value4).

    
    
    $data = ("Value1 Value2 Value3 Value4 Value5 Value6","Value1 Value2 Value3 Value4 Value5","Value1 Value2 Value3 Value4 Value5 Value6 Value7")
    
    ForEach ($Row in $data)
    {
        $i=1
        $FName = $null
                    
        While ($i -le "3")
        {    
            $Row = $Row.Substring($Row.IndexOf(" "))
            write-Host "Row: " $i $Row
            $i++                    
        }
    
        $FName = $Row
        write-Host "Full Name: "$FName
    }
    
    

    Thanks

  • #25015

    Participant
    Points: 1
    Rank: Member

    See how this does.

    $data = ("Value1 Value2 Value3 Value4 Value5 Value6","Value1 Value2 Value3 Value4 Value5","Value1 Value2 Value3 Value4 Value5 Value6 Value7")

    ForEach($row in $data) {
    $array = ($row -split "\s")
    $array[3..($array.Length – 1)] -join " "
    }

  • #25016

    Participant
    Points: 428
    Helping Hand
    Rank: Contributor

    If the only discernable pattern is the spaces, you could try something like this:

    $data = @("Value1 Value2 Value3 Value4 Value5 Value6","Value1 Value2 Value3 Value4 Value5","Value1 Value2 Value3 Value4 Value5 Value6 Value7")
    
    #We're going to generate a new object with only the values you want deemed $newData, catchy, I know...
    $newData = $data | foreach {
        #We would split the string into an array using the spaces as a delimiter
        $array = $_.ToString().Split(" ")
        #Create a new empty array
        $newArray = @()
        #Now that it's an array, we start at 0 which is Value1, 1 is Value 2, etc.
        #So, we start enumerating at index 3 which is Value4
        for($i = 3; $i -le ($array.Count -1);$i++) {
           #Add that index to a new array
           $newArray += $array[$i] 
        }
        #Re-join the array into a string using spaces and return it to $newData
        $newArray -join " "
    }
    
    $newData
    

    Output:

    Value4 Value5 Value6
    Value4 Value5
    Value4 Value5 Value6 Value7
    
  • #25021

    Participant
    Points: 1
    Rank: Member

    Thanks Guys – Both give me exactly what I'm after.

The topic ‘Substring to after the nth occurence of a character’ is closed to new replies.