LoginSignup
2
2

More than 3 years have passed since last update.

PowerShell 文字数を指定して変数に格納された文字列 を 取得する

Last updated at Posted at 2019-11-20

目的

  • PowerShellで変数に格納された文字列の文字数を指定して出力する方法を知る。

#書き方の例

  • 変数には任意の文字列が格納されているものとする。
  • 基準文字から何文字を出力、基準文字から後ろの文字を全て出力、文字列末尾から何文字を出力などなどいろいろなことができる。
  • 下記に当該処理の書き方の例を記載する。
# X文字目からY文字出力する
Write-Host $変数.Substring(X, Y);

# X文字目から後ろの文字全て出力する
Write-Host $変数.Substring(X);

# 文字列末尾からX文字を出力する
Write-Host $変数.Substring($変数.Length -X, X);

#より具体的な例

  • 変数numberには「123456789」が格納されているものとする。
  • 3文字目から6文字目までを出力、6文字目から後ろの文字を全て出力、文字列末尾から2文字を出力する方法を記載する
  • 下記に当該処理の書き方の例を記載する。
  • ->の後に記載されている文字は出力を表す。
# 変数への値の格納
$number = '123456789';

# 3文字目から6文字出力する
Write-Host $number.Substring(3, 6);
->456789

# 6文字目から後ろの文字全て出力する
Write-Host $number.Substring(6);
->789
# 文字列末尾から3文字を出力する
Write-Host $number.Substring($number.Length -2, 2);
->89
2
2
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
2
2