こんにちは!
プログラミング未経験文系出身、Elixirの国に迷い込んだ?!見習いアルケミストのaliceと申します。
今回はStringモジュールについて学んだことをまとめます。
目次
1.Stringモジュールで遊んでみたシリーズ① -String.at ~ String.chunk の紹介
2.Stringモジュールで遊んでみたシリーズ② -String.codepoints ~ String.ends_with? の紹介
3.Stringモジュールで遊んでみたシリーズ③ -String.equivalent? ~ String.last の紹介
4.Stringモジュールで遊んでみたシリーズ④ -String.length ~ String.next_grapheme の紹介
5.Stringモジュールで遊んでみたシリーズ⑤ -String.next_grapheme_size ~ String.printable? の紹介
6.Stringモジュールで遊んでみたシリーズ⑥ -String.replace ~ String.replace_suffix の紹介(本記事)
7.Stringモジュールで遊んでみたシリーズ⑦ -String.replace_trailing ~ String.split の紹介
8.Stringモジュールで遊んでみたシリーズ⑧ -String.split_at ~ String.to_charlist の紹介
9.Stringモジュールで遊んでみたシリーズ⑨ -String.to_existing_atom ~ String.trim の紹介
10.Stringモジュールで遊んでみたシリーズ10 -String.trim_leading ~ String.valid? の紹介
目的
Stringモジュールに含まれる関数を触って機能を理解したい
実行環境
Windows 11 + WSL2 + Ubuntu 22.04
Elixir v1.17.3
Erlang v27.0
String.replaceとは
String.replace(subject, pattern, replacement, options \\ [])
はsubject
内のpattern
をreplacement
に置き換えます。
例
String.replace("a,b,c", ",", "-")
"a-b-c"
global: false
のときはpattern
が複数回登場しても最初の1回のみ置き換えが行われます。
String.replace("a,b,c", ",", "-", global: false)
"a-b,c"
文字列リストを利用してpattern
に対して無名関数による操作をした場合
※下記の例では、patternにヒットした"a"と"c"について、それぞれ2文字後のアルファベットに置換する操作が行われます
String.replace("a,b,c", ["a", "c"], fn <<char>> -> <<char + 2>> end)
"c,b,e"
String.replace_invalidとは
String.replace_invalid(bytes, replacement \\ "�")
は、bytes
内の無効なバイトをreplacement
に置き換えます。
例
String.replace_invalid("asd" <> <<0xFF::8>>)
"asd�"
余談
バイトが無効な文字列かどうかはString.valid?
で確認することができます
String.valid?("asd" <> <<0xFF::8>>)
false
String.replace_leadingとは
String.replace_leading(string, match, replacement)
はsubject
内の前方一致検索を行い、match
にヒットした部分をreplacement
に置き換えます。
例
String.replace_leading("hello world", "hello ", "")
"world"
前方一致検索なので先頭にヒットしなければ置換されません
String.replace_leading("hey hello world", "hello ", "")
"hey hello world"
subject
の先頭からmatch
にヒットする要素が2回連続する場合全て置換されます
String.replace_leading("hello hello world", "hello ", "")
"world"
String.replace_prefixとは
String.replace_prefix(string, match, replacement)
はstring
内の前方一致検索を行いmatch
にヒットした場合その部分をreplacement
に置き換えます。
例
String.replace_leading("hello world", "hello ", "")
"world"
前方一致検索なので先頭にヒットしなければ置換されません
String.replace_prefix("hey hello world", "hello ", "")
"hey hello world"
string
の先頭からmatch
にヒットする要素が2回連続する場合先頭の1回だけ置換されます
String.replace_prefix("hello hello world", "hello ", "")
"hello world"
String.replace_suffixとは
String.replace_suffix(string, match, replacement)
はstring
内の後方一致検索を行いmatch
にヒットした場合その部分をreplacement
に置き換えます。
例
String.replace_suffix("hello world", " world", "")
"hello"
後方一致検索なので末尾にヒットしなければ置換されません
String.replace_suffix("hello world thanks", " world", "")
"hello world thanks"
string
の末尾からmatch
にヒットする要素が2回連続する場合末尾の1回だけ置換されます
String.replace_suffix("hello world world", " world", "")
"hello world"
~Elixirの国のご案内~
↓Elixirって何ぞや?と思ったらこちらもどぞ。Elixirは先端のアレコレをだいたい全部できちゃいます
↓ゼロからElixirを始めるなら「エリクサーチ」がおすすめ!私もエンジニア未経験から学習中です。
↓We Are The Alchemists, my friends!1
Elixirコミュニティは本当に優しくて温かい人たちばかり!
私が挫折せずにいられるのもこの恵まれた環境のおかげです。
まずは気軽にコミュニティを訪れてみてください。2
-
@torifukukaiouさんのAwesomeな名言をお借りしました。Elixirコミュニティを一言で表すと、これに尽きます。 ↩