What is the best way to trim characters?

Hello.
I want to trim “s” from this sentence.

"aassddaa"

Then, I use String.trim/2 to trim “s”.

iex(1)> a = "aassddaa"
"aassddaa"
iex(2)> b = String.trim(a, "s")
"aassddaa"

It does not work.
But “a” can be trimmed.

iex(3)> c = String.trim(a, "a")
"ssdd"

Why does it work like that?

Because trim removes trailing symbols.
You need String.replace/3:

> String.replace("aassddaa", "s", "")
"aaddaa"
4 Likes

Thank you for the quick answer!

@Papillon6814 Please read documentation for:

  1. String.replace/4
  2. String.replace_leading/3
  3. String.replace_prefix/3
  4. String.replace_suffix/3
  5. String.replace_trailing/3
  6. String.trim/2
  7. String.trim_leading/2
  8. String.trim_trailing/2
3 Likes

Trimming in this case just removes characters from the start and the end, not from anywhere inside the string.