2017-01-30 7 views
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 

答えて

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 
+1

を行うときには、トリムを実行することができ感謝@ Nkosi!複雑な思考の途中で簡単な答えを忘れることもあります。私は自分の状況に2番目のオプションを使用すると思います。 – JFV

関連する問題