- シングルクォート: 文字のリスト
- ダブルクォート: UTF-8エンコーディングされたバイト列のリスト
注意点
- バイナリサイズ ≠ 文字列のサイズ
- リストの文法だけでなく、バイナリの文法も意識する必要あり
> dqs = "∂x/∂y"
"∂x/∂y"
> String.length dqs
5
> byte_size dqs
9
> String.at(dqs, 0)
"∂"
> String.codepoints(dqs)
["∂", "x", "/", "∂", "y"]
> String.split(dqs, "/")
["∂x", "∂y"]
文字列とElixirのライブラリ Stringモジュール: ダブルクォート文字列で使う関数を定義
at(str, offset)
tring.at("∂og", 0)
"∂"
> String.at("∂og", -1)
"g"
capitalize(str)
> String.capitalize "école"
"École"
> String.capitalize "ÍÍÍÍÍ"
"Ííííí"
codepoints(str)
> String.codepoints "José's ∂φ g"
["J", "o", "s", "é", "'", "s", " ", "∂", "φ", " ", "g"]
downcase(str)
> String.downcase "Φ RSteD"
"φ rsted"
duplicate(str, n)
> String.duplicate "Ho! ", 3
"Ho! Ho! Ho! "
ends_with?(str, suffix | [suffixes])
> String.ends_with? "string", ["elix", "stri", "ring"]
true
> String.ends_with? "string", ["elix", "stri", "ling"]
false
first(str)
> String.first "∂og"
"∂"
graphemes(str)
> String.codepoints "noe\u0308l"
["n", "o", "e", "̈", "l"]
> String.graphemes "noe\u0308l"
["n", "o", "ë", "l"]
last(str)
> String.last "∂og"
"g"
length(str)
> String.length "∂x/∂y"
5
pad_trailing(str, new_length, padding \ " ")
ljust/2
ljust/3
はdeprecated
> String.pad_trailing("cat", 5)
"cat "
> String.pad_trailing("cat", 5, "^")
"cat^^"
> String.pad_trailing("cat", 2)
"cat"
> String.pad_trailing("cat", 2, "^")
"cat"
trim_leading(str)
lstrip/1
はdeprecated
> String.trim_leading "\t\f Hello\t\n"
"Hello\t\n"
trim_leading(str, to_trim)
to_trim
はStringを指定する
replace_leading/3
でも実現できる(ラッパー関数かも)
lstrip/2
はdeprecated
> String.trim_leading "!!!SALE!!!", "!"
"SALE!!!"
> String.replace_leading "!!!SALE!!!", "!", ""
"SALE!!!"
next_codepoint(str)
> MyString.each "∂og", fn c -> IO.puts c end
∂
o
g
[]
> MyString.each "noe\u0308l", &(IO.puts &1)
n
o
e
̈
l
[]
next_grapheme(str)
:no_grapheme
ではなく、 nil
を返すように変わったみたい
https://hexdocs.pm/elixir/master/String.html#next_grapheme/1
> MyString2.each "∂og", &(IO.puts &1)
∂
o
g
[]
> MyString2.each "noe\u0308l", &(IO.puts &1)
n
o
ë
l
[]
printable?(str)
\x{H*}
はdeprecated
\xH*
(byte) or \uHHHH
(codepoint) を使う
> String.printable? "José"
true
> String.printable? "\x0000 a null"
false
replace(str, pattern, replacement, options \ [global: true, insert_replacement: nil])
insert_replaced
の使い方がわかりにくい…
特にリストで指定された場合の動きが整理できていない
> String.replace "the cat on the mat", "at", "AT"
"the cAT on the mAT"
> String.replace "the cat on the mat", "at", "AT", global: false
"the cAT on the mat"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: 0
"the cAT on the mAT"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: 1
"the cAT on the mAT"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: 2
"the cAT on the mAT"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: 0
"the catAT on the matAT"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: 1
"the cAatT on the mAatT"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: 2
"the cATat on the mATat"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: 3
** (ArgumentError) argument error
(stdlib) binary.erl:275: :binary.replace/4
> String.replace "the cat on the mat", "at", "AT", insert_replaced: -1
** (ArgumentError) argument error
(stdlib) binary.erl:275: :binary.replace/4
> String.replace "the cat on the mat", "at", "AT", insert_replaced: [0, 0]
"the catatAT on the matatAT"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: [0, 1]
"the catAatT on the matAatT"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: [0, 2]
"the catATat on the matATat"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: [1, 0]
"the catAatT on the matAatT"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: [1, 1]
"the cAatatT on the mAatatT"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: [1, 2]
"the cAatTat on the mAatTat"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: [2, 0]
"the catATat on the matATat"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: [2, 1]
"the cAatTat on the mAatTat"
> String.replace "the cat on the mat", "at", "AT", insert_replaced: [2, 2]
"the cATatat on the mATatat"
> String.replace "the cat on the mat", "at", "BAT", insert_replaced: 0
"the catBAT on the matBAT"
> String.replace "the cat on the mat", "at", "BAT", insert_replaced: 1
"the cBatAT on the mBatAT"
> String.replace "the cat on the mat", "at", "BAT", insert_replaced: 2
"the cBAatT on the mBAatT"
> String.replace "the cat on the mat", "at", "BAT", insert_replaced: 3
"the cBATat on the mBATat"
> String.replace "the cat on the mat", "at", "BAT", insert_replaced: 4
** (ArgumentError) argument error
(stdlib) binary.erl:275: :binary.replace/4
reverse(str)
> String.reverse "pupils"
"slipup"
> String.reverse "Σf÷∂"
"∂÷fΣ"
pad_leading(str, new_length, padding \ " ")
rjust/3
はdeprecated
> String.pad_leading("cat", 5, ">")
">>cat"
> String.pad_leading("cat", 5)
" cat"
trim_trailing(str)
rstrip/1
はdeprecated
> String.trim_trailing " line \r\n"
" line"
trim_trailing(str, to_trim)
to_trim
はStringを指定する
rstrip/2
はdeprecated
tring.trim_trailing "!!!SALE!!!", "!"
"!!!SALE"
slice(str, offset, len)
> String.slice "the cat on the mat", 4, 3
"cat"
> String.slice "the cat on the mat", 18, 3
""
> String.slice "the cat on the mat", 17, 3
"t"
> String.slice "the cat on the mat", -3, 3
"mat"
split(str, pattern \ nil, options \ [parts: :infinity])
:parts
: 分割する数:trim
: 空文字の場合、削除するか否か
:global
オプションはなくなった?
https://hexdocs.pm/elixir/master/String.html#split/3
> String.split " the cat on the mat "
["the", "cat", "on", "the", "mat"]
> String.split "the cat on the mat", "t"
["", "he ca", " on ", "he ma", ""]
> String.split "the cat on the mat", "t", trim: true
["he ca", " on ", "he ma"]
> String.split "the cat on the mat", ~r{[ae]}
["th", " c", "t on th", " m", "t"]
> String.split "the cat on the mat", ~r{[ae]}, parts: 2
["th", " cat on the mat"]
> String.split "the cat on the mat", ~r{[ae]}, parts: 3
["th", " c", "t on the mat"]
starts_with?(str, prefix | [prefixes])
tring.starts_with? "string", ["elix", "stri", "ring"]
true
> String.starts_with? "string", ~w[elix stri ring]
true
> String.starts_with? "string", ~w[elix xir ing]
false
trim(str)
strip/1
はdeprecated
> String.trim "\t Hello \r\n"
"Hello"
trim(str, character)
strip/2
はdeprecated
> String.trim "!!!SALE!!!", "!"
"SALE"
upcase(str)
> String.upcase "José Φ rstüd"
"JOSÉ Φ RSTÜD"
valid?(str)
valid_character?
とは違い1文字でなくても true
になる
valid_character?/1
はdeprecated
> String.valid? "∂"
true
> String.valid? "∂og"
true
> String.valid? <<0x0308::16>>
true
> String.valid? <<0xFFFF::16>>
false