コンテンツにスキップ

Template:String split

テンプレートの解説[表示] [編集] [履歴] [キャッシュを破棄]

文字列を分割するためのテンプレートです。

使い方[編集]

{{Str count|入力文字列|区切り文字列|インデックス|プレーンフラグ}}

引数[編集]

入力文字列と区切り文字列は必須です。

  • |txt= - 入力文字列。エイリアス: |1=
  • |sep= - 区切り文字列。区切り文字列に含まれるダブルクォートは分割前に削除されるため、内部リンクを探す場合に["[のパターンを使用できます。パイプを区切り文字列に含まれる場合は{{!}}のように入力します。エイリアス: |2=
  • |idx= - 分割後の文字列インデックス。インデックスは1から始まります。負の値の場合は後ろから数えます。分割後の文字列入力しなかった場合の既定値は1。エイリアス: |3=
  • |plain= - ブーリアン型のフラグで、trueの場合|sep=がプレーンテキストとして、falseの場合ScribuntoのUstringパターンLua正規表現の書式でUnicode使用可)として認識されます。既定値はtrue。エイリアス: |4=

使用例[編集]

プレーンテキストで分割する場合:

  • {{String split|This is a piece of text to be split |" "}} → This
  • {{String split|This is a piece of text to be split |" "| 4}} → piece
  • {{String split|This is a piece of text to be split |x| 2}} → t to be split

ScribuntoのUstringパターンを使用する場合:

  • {{String split|Apples, pears, oranges; Cats, dogs|"%p"| 2 |false}} → pears
  • {{String split|Apples, pears, oranges; Cats, dogs|"%p"| 4 |false}} → Cats
  • {{String split|Apples pears oranges; Cats dogs|"%A+"| 4 |no}} → Cats

インデックスが負の値の場合は後ろから数えます。

  • {{String split|txt=This is a piece of text to be split |sep=" " |idx=-1}} → split

名前付き引数で入力文字列を渡した場合、前後のスペースは自動で除去されます。

  • {{String split| txt=Apples pears oranges; Cats dogs | sep="%A+" | idx=3 | plain=false }} → oranges

名無し引数で入力文字列を渡した場合、前後のスペースは自動で除去されません。

  • {{String split|This is a piece of text to be split |" "|-1}}
  • {{String split|This is a piece of text to be split|" "|-1}} → split

関連項目[編集]