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

書式
文字列.Trim()
Trimメソッドで先頭の半角空白と末尾の連続した半角空白を取り除きます
Trimメソッドで先頭の半角空白と末尾の連続した半角空白を取り除
先頭と末尾の全角空白を取り除きます。
空白が複数の場合、複数の空白を取り除きます。
使用例

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

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

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

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

実行結果

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

 

PowerShell

Posted by arkgame