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

标签 string powershell newline powershell-5.0

我正在 Powershell 中为此处字符串中的特定单词着色。除了其中包含回车/换行符的单词外,它都有效。如果没有这些字符,如何计算单词的长度?

下面是我正在使用的函数和测试数据。我希望第二行上的“is”也被着色,但我相信回车/换行符导致了长度不匹配的问题。

我感谢您提供的任何和所有帮助!谢谢! -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

最佳答案

在使用每个单词之前尝试修剪它,这样空格就不再是一个因素

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

关于string - Powershell函数捕获字符串长度而不返回/换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41938151/

相关文章:

command-line - 如何在特定标记后搜索和替换空行?

c# - 用换行符将字符串拆分成多个字符串?

java - 字符串中的字符重复整数倍

swift - 字符串(contentsOfURL :) in Swift 3

excel - Powershell 中的 VLOOKUP 脚本

winforms - Parent 如何在 System.Windows.Forms 中工作

java - 使用正则表达式分割字符串,但在子字符串中包含部分正则表达式

c - scanf()读取自己的输入和后面scanf()的输入

powershell - 如何为 Powershell 函数设置别名?

比较两个没有 CR LF 的字符数组