Erlang の string モジュールを一通り試す - その1
OTP 20.0 で string モジュールに多くの関数が追加されていたのに、最近気がついた。一通り試そうと思う。
string モジュールのおさらいをしたい人は下記の記事へ
casefold/1 関数
casefold(String :: unicode:chardata()) -> unicode:chardata()
文字列を大文字と小文字を区別しない比較可能な文字列に変換する。 2つの文字列が等しいかどうかを比較する場合は、casefold/1
関数がlowercase/1
よりも良いです。 equal/4
も参照すること。
Example:
> string:casefold("Hello World!"). "hello world!"
chomp/1 関数
chomp(String :: unicode:chardata()) -> unicode:chardata()
文字列から末尾の \n
または \r\n
が削除されたものを返す。
Example:
> string:chomp(<<"\nHello\n\n">>). <<"\nHello">> > string:chomp("\nHello\n\n"). "\nHello" > string:chomp("\nHello\r\n\r\n"). "\nHello" > string:chomp("\nHello\r\r\n"). "\nHello\r"
連続して改行がある場合、それらは削除される。
equal/2, equal/3, equal/4 関数
equal(A, B) -> boolean() equal(A, B, IgnoreCase) -> boolean() equal(A, B, IgnoreCase, Norm) -> boolean()
Types
A = B = unicode:chardata() IgnoreCase = boolean() Norm = none | nfc | nfd | nfkc | nfkd
AとBが等しい場合はtrue、そうでない場合はfalseを返す。
IgnoreCaseがtrueの場合、関数は等価性テストの前にその場で casefold を行う。
Normがnoneでない場合、関数は等価性テストの前にその場で正規化を適用する。
4つの使用可能な正規化形式がある。nfc、nfd、nfkc、およびnfkd。 デフォルトでは、IgnoreCaseはfalseで、Normはnoneである。
Example:
> string:equal("abc", "ABC"). false > string:equal("abc", "ABC", true). true > string:equal("åäö", unicode:characters_to_nfd_binary("åäö")). false > string:equal("åäö", unicode:characters_to_nfd_binary("åäö"), true). false > string:equal("åäö", unicode:characters_to_nfd_binary("åäö"), true, nfd). true
find/2, find/3 関数
find(String, SearchPattern) -> unicode:chardata() | nomatch find(String, SearchPattern, Dir) -> unicode:chardata() | nomatch
Types
String = SearchPattern = unicode:chardata() Dir = direction()
String の中から SearchPattern を探す。SearchPattern が見つかった場合は、その前にあるものをすべて削除した String の残りを返す。SearchPattern が見つからない場合は nomatch を返す。 Dir(先頭または末尾)は、文字が検索される方向を示す。 デフォルトでは、Dir は leading。
Example:
> string:find("foo, bar, baz", "ba"). "bar, baz" > string:find("foo, bar, baz", "ba", trailing). "baz" > string:find("foo, bar, baz", "qux"). nomatch > string:find(<<"foo, bar, baz">>, "ba", leading). <<"bar, baz">>
is_empty/1 関数
is_empty(String :: unicode:chardata()) -> boolean()
String が空の文字列の場合は true を返し、そうでない場合は false を返す。
Example:
> string:is_empty("foo"). false > string:is_empty(""). true > string:is_empty(<<"">>). true > string:is_empty(<<>>). true > string:is_empty(["",<<>>]). true > string:is_empty(["foo",<<>>]). false > string:is_empty(["foo",<<"baz">>]). false
length/1 関数
length(String :: unicode:chardata()) -> integer() >= 0
String の書記素クラスタの数を返す。
Example:
> string:length("ß↑e̊"). 3 > io:format("~p~n",["ß↑e̊"]). [223,8593,101,778] ok > string:length([223,8593,101,778]). 3 > binary_to_list(unicode:characters_to_binary("ß↑e̊")). [195,159,226,134,145,101,204,138] > string:length(<<195,159,226,134,145,101,204,138>>). 3
lexemes/2 関数
lexemes(String :: unicode:chardata(), SeparatorList :: [grapheme_cluster()]) -> [unicode:chardata()]
SeparatorList の書記素クラスタによって区切られた文字列の語彙素のリストを返す。
下記の例のように、String内の2つ以上の隣接するセパレータの書記素クラスタは1つとして扱われることに注意する。そのため、結果のリストには空文字列はない。空文字列を返す split/3
も参照する。
[$\r,$\n]
はひとつの書記素クラスタであることに注意する。
Example:
> string:lexemes("abc de̊fxxghix jkl\r\nfoo", "x e" ++ [[$\r,$\n]]). ["abc","de̊f","ghi","jkl","foo"] > string:lexemes(<<"abc de̊fxxghix jkl\r\nfoo"/utf8>>, "x e" ++ [$\r,$\n]). [<<"abc">>,<<"de̊f"/utf8>>,<<"ghi">>,<<"jkl\r\nfoo">>]
lowercase/1 関数
lowercase(String :: unicode:chardata()) -> unicode:chardata()
文字列を小文字に変換する。
ただし、等しいかどうかテストするために文字列を変換するときは、関数 casefold/1
を使う。
Example:
> string:lowercase("Hello World"). "hello world"
他の記事へ