「PowerShell」TrimEndメソッドで文字列末尾の空白を取り除く

書式
文字列.TrimEnd()
TrimEndメソッドを利用して文字列の末尾の空白を取り除きます。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 全角空白あり
$SS = " test abc "
Write-Host($SS.Length)
Write-Host($SS.TrimEnd())
Write-Host($SS.TrimEnd().Length)
# 半角空白あり
$tt = " test abc "
Write-Host($tt.Length)
Write-Host($tt.TrimEnd())
Write-Host($tt.TrimEnd().Length)
# 全角空白あり $SS = " test abc " Write-Host($SS.Length) Write-Host($SS.TrimEnd()) Write-Host($SS.TrimEnd().Length) # 半角空白あり $tt = " test abc " Write-Host($tt.Length) Write-Host($tt.TrimEnd()) Write-Host($tt.TrimEnd().Length)
# 全角空白あり
$SS = " test abc "

Write-Host($SS.Length) 
Write-Host($SS.TrimEnd()) 
Write-Host($SS.TrimEnd().Length) 

# 半角空白あり
$tt = " test abc  "

Write-Host($tt.Length) 
Write-Host($tt.TrimEnd()) 
Write-Host($tt.TrimEnd().Length)

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> .\test.ps1
10
 test abc
9
11
test abc
9
> .\test.ps1 10  test abc 9 11 test abc 9
> .\test.ps1
10
 test abc
9
11
 test abc
9

 

PowerShell

Posted by arkgame