Powershell函数捕获没有Return/Newline的字符串长度
Powershell Function to capture length of string without Return/Newline
我正在使用 Powershell 为 here-string 中的特定单词着色。除了其中包含 Return/Newline 个字符的单词外,它的工作正常。如果没有这些字符,我如何计算单词的长度?
下面是我正在使用的函数和测试数据。我希望第二行的 '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
尝试 trim 在使用每个单词之前记住它,这样白色 space 就不是一个因素
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
否则,您可以在进行长度比较时执行 trim
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
我正在使用 Powershell 为 here-string 中的特定单词着色。除了其中包含 Return/Newline 个字符的单词外,它的工作正常。如果没有这些字符,我如何计算单词的长度?
下面是我正在使用的函数和测试数据。我希望第二行的 '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
尝试 trim 在使用每个单词之前记住它,这样白色 space 就不是一个因素
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
否则,您可以在进行长度比较时执行 trim
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