1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

terraformのsubstr関数について

Last updated at Posted at 2021-03-09

使い方でちょっと迷ったのでメモ。

3つの引数が必要なことと、文字を出力することしかわからなかったのでterraform consoleコマンドを使ってsubstr関数がどんなものか試した。

注意

この記事はTerraform v0.12.24 で実行したものです。
1.0.6では「全ての文字列を取得する。」項目で実行してるコマンドで想定の結果にならないことを確認しています

substrとは

任意の文字列から指定した数値の文字から指定した文字の値までを取得する関数。
substrは3つの引数を使用していて、

substr("文字列", 数値, 数値)で記載する必要がある。内訳は、

substr(検索したい任意の文字列, 何文字目から取得するかの指定, 何文字目までを取得するかの指定)

これだけだと味気ないのでterraform consoleコマンドで試してみる。

検証

terraform consoleコマンドで関数を試せるようにする。

terraform console
>

1文字目から10文字目までを取得する。

注意したいのが、1文字目を取得する時は1でなく0を使用する。

> substr("test terraform", 0, 10)
test terra

全ての文字列を取得する。

引数の数値を全て0にすれば文字列全ての値を取得する。

> substr("test terraform", 0, 0)
test terraform
>

終わり

この関数について日本語の記事が無かったので作成。
実際使ってみないとわからない仕様もあったので試して良かった。

1
0
4

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?