LoginSignup
5
9

More than 5 years have passed since last update.

PowerShellでコード内に書いた複数行のテキストを手っ取り早く配列にする

Posted at

PowerShellでコード内に複数行のテキストを書いて、
1行1要素のリストに変換したい場合のコードです。
わざわざテキストファイルに書き出したり、
配列の書式で書くのが手間なときに便利です。

textlist.ps1
# 定義
$textlist  = @"
手っ取り早く
複数行のテキストを
リストにしたい。
ここにテキストを
貼り付けるイメージです。
"@ -split "`r`n"

# 1要素数ずつ表示
foreach($t in $textlist){
  echo("text: " + $t)
} 

# タイプを表示
echo($textlist.GetType())

# 要素数を表示
echo("Length: " + $textlist.Length)
実行結果
text: 手っ取り早く
text: 複数行のテキストを
text: リストにしたい。
text: ここにテキストを
text: 貼り付けるイメージです。

IsPublic IsSerial Name                                     BaseType                   
-------- -------- ----                                     --------                   
True     True     String[]                                 System.Array               
Length: 5

参考

5
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
9