Search code examples
stringpowershellnewlinepowershell-5.0

Powershell Function to capture length of string without Return/Newline


I'm working within Powershell to color specific words within a here-string. It's working except for words that have the Return/Newline characters within. How can I compute the length of a word without these characters?

Below is the Function I'm using and test data. I would like the 'is' on the second line to also be colored, but I believe that the Return/Newline is causing the issue with the length mismatch.

I appreciate any and all help that could be provided! Thanks! -JFV

Function ColorSpecificWordsOutput {
param(
    [Parameter(Mandatory=$true, Position=0)]
    [string]$InputText, 

    [Parameter(Mandatory=$true, Position=1)]
    $KeyColor
    )

$keys = $keycolor.keys -join "|"

#Split on spaces, pipe to foreach-object
$InputText.Split(" ") | ForEach-Object {
    #If word matches one of the $keys
    If ($_ -imatch $keys) {
        #Retrieve word as string from $keys
        [string]$m = $matches.Values[0].trim()

        #If length of word equals the $keys word
        If($_.Length -eq $m.Length) {
            #Write out the word with the mapped forground color without a new line
            Write-Host "$_ " -ForegroundColor $keyColor.item($m) -NoNewline
        }
        #Otherwise, just write the word without color
        Else { Write-Host "$_ " -NoNewline }
    }
    #Otherwise, just write the word without color
    else {
        Write-Host "$_ " -NoNewline
    }
}
}

$w = @"
This color is Yellow: test 
Is it correct ?
"@

$find = @{
is = "Cyan"
test = "Yellow"
correct = "Green"
}

ColorSpecificWordsOutput -InputText $w -KeyColor $find

Solution

  • Try trimming each word before using it so the white space is not a factor

    Function ColorSpecificWordsOutput {
    param(
        [Parameter(Mandatory=$true, Position=0)]
        [string]$InputText, 
    
        [Parameter(Mandatory=$true, Position=1)]
        $KeyColor
        )
    
        $keys = $keycolor.keys -join "|"
    
        #Split on spaces, pipe to foreach-object
        $InputText.Split(" ") | ForEach-Object {
    
            $word = $_.Trim() # Trim current word
    
            #If word matches one of the $keys
            If ($word -imatch $keys) {
                #Retrieve word as string from $keys
                [string]$m = $matches.Values[0].trim()
    
                #If length of word equals the $keys word
                If($word.Length -eq $m.Length) {
                    #Write out the word with the mapped forground color without a new line
                    Write-Host "$word " -ForegroundColor $keyColor.item($m) -NoNewline
                }
                #Otherwise, just write the word without color
                Else { Write-Host "$word " -NoNewline }
            }
            #Otherwise, just write the word without color
            else {
                Write-Host "$word " -NoNewline
            }
        }
    }
    
    $w = @"
    This color is Yellow: test 
    Is it correct ?
    "@
    
    $find = @{
    is = "Cyan"
    test = "Yellow"
    correct = "Green"
    }
    
    ColorSpecificWordsOutput -InputText $w -KeyColor $find
    

    Other wise you could perform the trim when doing the length comparison

    Function ColorSpecificWordsOutput {
    param(
        [Parameter(Mandatory=$true, Position=0)]
        [string]$InputText, 
    
        [Parameter(Mandatory=$true, Position=1)]
        $KeyColor
        )
    
        $keys = $keycolor.keys -join "|"
    
        #Split on spaces, pipe to foreach-object
        $InputText.Split(" ") | ForEach-Object {
            $word = $_
            #If word matches one of the $keys
            If ($word -imatch $keys) {
                #Retrieve word as string from $keys
                [string]$m = $matches.Values[0].trim()
    
                #If length of word equals the $keys word
                If($word.Trim().Length -eq $m.Length) {#performing trim before comparison
                    #Write out the word with the mapped forground color without a new line
                    Write-Host "$word " -ForegroundColor $keyColor.item($m) -NoNewline
                }
                #Otherwise, just write the word without color
                Else { Write-Host "$word " -NoNewline }
            }
            #Otherwise, just write the word without color
            else {
                Write-Host "$word " -NoNewline
            }
        }
    }
    
    $w = @"
    This color is Yellow: test 
    Is it correct ?
    "@
    
    $find = @{
    is = "Cyan"
    test = "Yellow"
    correct = "Green"
    }
    
    ColorSpecificWordsOutput -InputText $w -KeyColor $find