p-string
string モジュールは文字列操作のためのユーティリティ関数を提供します。
use p-string as slen[str]
Section titled “len[str]”文字列の長さを返します。
use p-string as ss.len[///hello///] -- 5contains[str; sub]
Section titled “contains[str; sub]”str が sub を含んでいれば true を返します。
use p-string as ss.contains[///hello world///; ///world///] -- truestartswith[str; prefix]
Section titled “startswith[str; prefix]”str が prefix で始まれば true を返します。
endswith[str; suffix]
Section titled “endswith[str; suffix]”str が suffix で終われば true を返します。
use p-string as ss.startswith[///hello///; ///he///] -- trues.endswith[///hello///; ///lo///] -- trueindexof[str; sub]
Section titled “indexof[str; sub]”str 内で最初に sub が出現するインデックスを返します。見つからない場合は -1。
use p-string as ss.indexof[///hello///; ///ll///] -- 2s.indexof[///hello///; ///xyz///] -- -1count[str; sub]
Section titled “count[str; sub]”str 内の sub の重複しない出現回数を返します。
use p-string as ss.count[///banana///; ///an///] -- 2upper[str]
Section titled “upper[str]”文字列を大文字に変換して返します。
lower[str]
Section titled “lower[str]”文字列を小文字に変換して返します。
use p-string as ss.upper[///hello///] -- "HELLO"s.lower[///HELLO///] -- "hello"capitalize[str]
Section titled “capitalize[str]”文字列の最初の文字を大文字にして返します。
title[str]
Section titled “title[str]”各単語の最初の文字を大文字にして返します。
use p-string as ss.capitalize[///hello world///] -- "Hello world"s.title[///hello world///] -- "Hello World"trim[str]
Section titled “trim[str]”両端の空白を除去します。
trimstart[str]
Section titled “trimstart[str]”先頭の空白を除去します。
trimend[str]
Section titled “trimend[str]”末尾の空白を除去します。
use p-string as ss.trim[/// hello ///] -- "hello"s.trimstart[/// hello ///] -- "hello "s.trimend[/// hello ///] -- " hello"reverse[str]
Section titled “reverse[str]”文字列を反転して返します。
use p-string as ss.reverse[///hello///] -- "olleh"repeat[str; n]
Section titled “repeat[str; n]”文字列を n 回繰り返して返します。
use p-string as ss.repeat[///ab///; 3] -- "ababab"replace[str; old; new]
Section titled “replace[str; old; new]”old のすべての出現を new に置換します。
replacefirst[str; old; new]
Section titled “replacefirst[str; old; new]”old の最初の出現を new に置換します。
use p-string as ss.replace[///aabaa///; ///a///; ///x///] -- "xxbxx"s.replacefirst[///aabaa///; ///a///; ///x///] -- "xabaa"padstart[str; len; fill]
Section titled “padstart[str; len; fill]”文字列の先頭を len に達するまでパディングします。fill 文字のデフォルトはスペースです。
padend[str; len; fill]
Section titled “padend[str; len; fill]”文字列の末尾を len に達するまでパディングします。fill 文字のデフォルトはスペースです。
use p-string as ss.padstart[///42///; 5; ///0///] -- "00042"s.padend[///hi///; 5; ///.///] -- "hi..."分割 / 結合
Section titled “分割 / 結合”split[str; sep]
Section titled “split[str; sep]”文字列を sep で分割し、配列を返します。
lines[str]
Section titled “lines[str]”文字列を改行で分割します。
words[str]
Section titled “words[str]”文字列を空白で分割し、空のセグメントを無視します。
join[arr; sep]
Section titled “join[arr; sep]”文字列の配列を sep で結合します。
chars[str]
Section titled “chars[str]”個々の文字の配列を返します。
use p-string as ss.split[///a,b,c///; ///,///] -- ["a", "b", "c"]s.words[///hello world///] -- ["hello", "world"]s.join[[///a///; ///b///]; ///-///] -- "a-b"s.chars[///hi///] -- ["h", "i"]slice[str; start; end]
Section titled “slice[str; start; end]”start から end(排他的)までの部分文字列を返します。
charat[str; i]
Section titled “charat[str; i]”インデックス i の文字を返します。
codeat[str; i]
Section titled “codeat[str; i]”インデックス i のUnicodeコードポイントを返します。
fromcode[code]
Section titled “fromcode[code]”指定されたUnicodeコードポイントの文字を返します。
use p-string as ss.slice[///hello///; 1; 3] -- "el"s.charat[///hello///; 0] -- "h"s.codeat[///A///; 0] -- 65s.fromcode[65] -- "A"