gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:23:59 27 4
gpt4 key购买 nike

我正在Powershell中工作,以为here字符串中的特定单词着色。除包含回车/换行符的单词外,其他功能均有效。没有这些字符,如何计算单词的长度?

以下是我正在使用的功能和测试数据。我希望第二行上的"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

最佳答案

尝试在使用每个单词之前先对其进行修整,这样空格就不会成为问题

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com