Powershell函数捕获字符串长度而不返回/换行符

时间:2017-01-30 14:20:27

标签: string powershell newline powershell-v5.0

我在Powershell中工作,为here-string中的特定单词着色。它的工作除外,其中包含返回/换行符。如何在没有这些字符的情况下计算单词的长度?

以下是我使用和测试数据的功能。我希望'是'在第二行也有颜色,但我相信返回/换行导致长度不匹配的问题。

我感谢所有可以提供的帮助!谢谢! -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

1 个答案:

答案 0 :(得分:3)

在使用之前尝试修剪每个单词,因此空白区域不是一个因素

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

另外,你可以在进行长度比较时执行修剪

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