10
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Stringモジュールで遊んでみたシリーズ⑥ -String.replace ~ String.replace_suffix の紹介

Last updated at Posted at 2024-12-10

こんにちは!
プログラミング未経験文系出身、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内のpatternreplacementに置き換えます。

iex
String.replace("a,b,c", ",", "-")
"a-b-c"

global: falseのときはpatternが複数回登場しても最初の1回のみ置き換えが行われます。

iex
String.replace("a,b,c", ",", "-", global: false)
"a-b,c"

文字列リストを利用してpatternに対して無名関数による操作をした場合
※下記の例では、patternにヒットした"a"と"c"について、それぞれ2文字後のアルファベットに置換する操作が行われます

iex
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に置き換えます。

iex
String.replace_invalid("asd" <> <<0xFF::8>>)
"asd�"

余談

バイトが無効な文字列かどうかはString.valid?で確認することができます

iex
String.valid?("asd" <> <<0xFF::8>>)
false

String.replace_leadingとは

String.replace_leading(string, match, replacement)subject内の前方一致検索を行い、matchにヒットした部分をreplacementに置き換えます。

iex
String.replace_leading("hello world", "hello ", "")
"world"

前方一致検索なので先頭にヒットしなければ置換されません

iex
String.replace_leading("hey hello world", "hello ", "")
"hey hello world"

subjectの先頭からmatchにヒットする要素が2回連続する場合全て置換されます

iex
String.replace_leading("hello hello world", "hello ", "")
"world"

String.replace_prefixとは

String.replace_prefix(string, match, replacement)string内の前方一致検索を行いmatchにヒットした場合その部分をreplacementに置き換えます。

iex
String.replace_leading("hello world", "hello ", "")
"world"

前方一致検索なので先頭にヒットしなければ置換されません

iex
String.replace_prefix("hey hello world", "hello ", "")
"hey hello world"

stringの先頭からmatchにヒットする要素が2回連続する場合先頭の1回だけ置換されます

iex
String.replace_prefix("hello hello world", "hello ", "")
"hello world"

String.replace_suffixとは

String.replace_suffix(string, match, replacement)string内の後方一致検索を行いmatchにヒットした場合その部分をreplacementに置き換えます。

iex
String.replace_suffix("hello world", " world", "")
"hello"

後方一致検索なので末尾にヒットしなければ置換されません

iex
String.replace_suffix("hello world thanks", " world", "")
"hello world thanks"

stringの末尾からmatchにヒットする要素が2回連続する場合末尾の1回だけ置換されます

iex
String.replace_suffix("hello world world", " world", "")
"hello world"

~Elixirの国のご案内~

↓Elixirって何ぞや?と思ったらこちらもどぞ。Elixirは先端のアレコレをだいたい全部できちゃいます:laughing::sparkles::sparkles:

↓ゼロからElixirを始めるなら「エリクサーチ」がおすすめ!私もエンジニア未経験から学習中です。

We Are The Alchemists, my friends!:bouquet:1
Elixirコミュニティは本当に優しくて温かい人たちばかり!
私が挫折せずにいられるのもこの恵まれた環境のおかげです。
まずは気軽にコミュニティを訪れてみてください。2

  1. @torifukukaiouさんのAwesomeな名言をお借りしました。Elixirコミュニティを一言で表すと、これに尽きます。

  2. @kn339264さんの素敵なスライドをお借りしました。Elixirコミュニティはいろんな形で活動中!

10
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?