2
Powershell内で、here-string内の特定の単語に色を付けるために作業しています。それは、Return/Newline文字を含む単語を除いて動作しています。これらの文字がない単語の長さはどのように計算できますか?Powershell Return /改行なしで文字列の長さを取得する機能
以下は、私が使用しているデータとテストデータです。私は2行目の「is」も色付けしたいと思っていますが、Return/Newlineは長さの不一致を引き起こしていると思います。
私は何かすべてのヘルプを提供していただきありがとうございます!ありがとう! -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
を行うときには、トリムを実行することができ感謝@ Nkosi!複雑な思考の途中で簡単な答えを忘れることもあります。私は自分の状況に2番目のオプションを使用すると思います。 – JFV