Elixir字符串 #
一、字符串基础 #
1.1 字符串定义 #
Elixir字符串是UTF-8编码的字节序列:
elixir
iex(1)> "Hello, World!"
"Hello, World!"
iex(2)> "你好,世界!"
"你好,世界!"
1.2 字符串本质 #
字符串实际上是二进制:
elixir
iex(1)> is_binary("Hello")
true
iex(2)> is_string("Hello")
false
1.3 字符串长度 #
elixir
iex(1)> String.length("Hello")
5
iex(2)> String.length("你好")
2
iex(3)> byte_size("Hello")
5
iex(4)> byte_size("你好")
6
1.4 转义字符 #
elixir
iex(1)> "Hello\nWorld"
"Hello\nWorld"
iex(2)> "Tab\tHere"
"Tab\tHere"
iex(3)> "Quote: \""
"Quote: \""
iex(4)> "Backslash: \\"
"Backslash: \\"
1.5 多行字符串 #
elixir
iex(1)> """
...> Line 1
...> Line 2
...> Line 3
...> """
"Line 1\nLine 2\nLine 3\n"
二、字符串插值 #
2.1 基本插值 #
elixir
iex(1)> name = "World"
"World"
iex(2)> "Hello, #{name}!"
"Hello, World!"
2.2 表达式插值 #
elixir
iex(1)> a = 10
10
iex(2)> b = 20
20
iex(3)> "Sum: #{a + b}"
"Sum: 30"
2.3 原子插值 #
elixir
iex(1)> status = :ok
:ok
iex(2)> "Status: #{status}"
"Status: ok"
三、字符串操作 #
3.1 拼接 #
elixir
iex(1)> "Hello, " <> "World!"
"Hello, World!"
iex(2)> name = "Alice"
"Alice"
iex(3)> "Hello, " <> name
"Hello, Alice"
3.2 大小写转换 #
elixir
iex(1)> String.upcase("hello")
"HELLO"
iex(2)> String.downcase("HELLO")
"hello"
iex(3)> String.capitalize("hello world")
"Hello world"
iex(4)> String.titlecase("hello world")
"Hello World"
3.3 去除空白 #
elixir
iex(1)> String.trim(" hello ")
"hello"
iex(2)> String.trim_leading(" hello ")
"hello "
iex(3)> String.trim_trailing(" hello ")
" hello"
iex(4)> String.trim("xxhelloxx", "x")
"hello"
3.4 分割与连接 #
elixir
iex(1)> String.split("a,b,c", ",")
["a", "b", "c"]
iex(2)> String.split("a b c")
["a", "b", "c"]
iex(3)> Enum.join(["a", "b", "c"], ",")
"a,b,c"
iex(4)> Enum.join(["a", "b", "c"])
"abc"
3.5 包含检查 #
elixir
iex(1)> String.contains?("Hello, World!", "World")
true
iex(2)> String.contains?("Hello, World!", "world")
false
iex(3)> String.contains?("Hello", ["World", "Hello"])
true
iex(4)> String.starts_with?("Hello", "He")
true
iex(5)> String.ends_with?("Hello", "lo")
true
3.6 查找与替换 #
elixir
iex(1)> String.replace("Hello, World!", "World", "Elixir")
"Hello, Elixir!"
iex(2)> String.replace("aaa", "a", "b")
"bbb"
iex(3)> String.replace("aaa", "a", "b", global: false)
"baa"
iex(4)> String.slice("Hello, World!", 0, 5)
"Hello"
iex(5)> String.slice("Hello, World!", 7..11)
"World"
iex(6)> String.at("Hello", 0)
"H"
iex(7)> String.at("Hello", -1)
"o"
3.7 重复与反转 #
elixir
iex(1)> String.duplicate("abc", 3)
"abcabcabc"
iex(2)> String.reverse("Hello")
"olleH"
iex(3)> String.pad_leading("5", 3, "0")
"005"
iex(4)> String.pad_trailing("5", 3, "0")
"500"
3.8 字符串遍历 #
elixir
iex(1)> String.graphemes("Hello")
["H", "e", "l", "l", "o"]
iex(2)> String.codepoints("Hello")
["H", "e", "l", "l", "o"]
iex(3)> String.to_charlist("Hello")
'Hello'
iex(4)> for <<char <- "Hello">>, do: char
'Hello'
四、字符列表 #
4.1 字符列表定义 #
字符列表是字符编码的列表:
elixir
iex(1)> 'Hello'
'Hello'
iex(2)> is_list('Hello')
true
iex(3)> hd('Hello')
72
iex(4)> [72, 101, 108, 108, 111]
'Hello'
4.2 字符串与字符列表的区别 #
elixir
iex(1)> "Hello"
"Hello"
iex(2)> 'Hello'
'Hello'
iex(3)> is_binary("Hello")
true
iex(4)> is_list('Hello')
true
4.3 相互转换 #
elixir
iex(1)> String.to_charlist("Hello")
'Hello'
iex(2)> List.to_string('Hello')
"Hello"
iex(3)> to_charlist("Hello")
'Hello'
iex(4)> to_string('Hello')
"Hello"
4.4 字符列表使用场景 #
字符列表主要用于与Erlang库交互:
elixir
iex(1)> :io.format('Hello, ~s!~n', ['World'])
Hello, World!
:ok
五、二进制 #
5.1 二进制定义 #
elixir
iex(1)> <<1, 2, 3>>
<<1, 2, 3>>
iex(2)> <<255, 255, 255>>
<<255, 255, 255>>
iex(3)> is_binary(<<1, 2, 3>>)
true
5.2 字符串与二进制 #
字符串是UTF-8编码的二进制:
elixir
iex(1)> <<72, 101, 108, 108, 111>>
"Hello"
iex(2)> <<104, 101, 108, 108, 111>>
"hello"
5.3 二进制模式匹配 #
elixir
iex(1)> <<head, rest::binary>> = "Hello"
"Hello"
iex(2)> head
72
iex(3)> rest
"ello"
iex(4)> <<a::size(8), b::size(8), c::size(8)>> = <<1, 2, 3>>
<<1, 2, 3>>
iex(5)> {a, b, c}
{1, 2, 3}
5.4 位串操作 #
elixir
iex(1)> <<1::size(4), 2::size(4)>>
<<18>>
iex(2)> <<1::size(8)>>
<<1>>
iex(3)> <<1::size(16)>>
<<0, 1>>
iex(4)> <<255::size(16)>>
<<0, 255>>
六、Sigils #
6.1 字符串Sigil #
elixir
iex(1)> ~s(Hello, World!)
"Hello, World!"
iex(2)> ~s(Hello #{name})
"Hello Alice"
iex(3)> ~S(Hello #{name})
"Hello \#{name}"
6.2 字符列表Sigil #
elixir
iex(1)> ~c(Hello)
'Hello'
iex(2)> ~c(Hello #{name})
'Hello Alice'
iex(3)> ~C(Hello #{name})
'Hello \#{name}'
6.3 正则表达式Sigil #
elixir
iex(1)> ~r/hello/
~r/hello/
iex(2)> Regex.match?(~r/hello/, "hello world")
true
iex(3)> Regex.replace(~r/hello/, "hello world", "hi")
"hi world"
6.4 单词列表Sigil #
elixir
iex(1)> ~w(hello world)
["hello", "world"]
iex(2)> ~w(hello world)a
[:hello, :world]
iex(3)> ~w(hello world)c
['hello', 'world']
iex(4)> ~W(hello #{name})
["hello", "\#{name}"]
七、正则表达式 #
7.1 基本匹配 #
elixir
iex(1)> Regex.match?(~r/hello/, "hello world")
true
iex(2)> Regex.match?(~r/^hello/, "hello world")
true
iex(3)> Regex.match?(~r/world$/, "hello world")
true
7.2 捕获组 #
elixir
iex(1)> Regex.run(~r/(\d+)-(\d+)/, "Phone: 123-456")
["123-456", "123", "456"]
iex(2)> Regex.named_captures(~r/(?<area>\d+)-(?<number>\d+)/, "Phone: 123-456")
%{"area" => "123", "number" => "456"}
7.3 替换 #
elixir
iex(1)> Regex.replace(~r/\d+/, "Price: 100 dollars", "XXX")
"Price: XXX dollars"
iex(2)> Regex.replace(~r/\d+/, "Price: 100 dollars", fn match ->
...> String.duplicate("X", String.length(match))
...> end)
"Price: XXX dollars"
7.4 分割 #
elixir
iex(1)> Regex.split(~r/[, ]+/, "a, b, c")
["a", "b", "c"]
八、字符串编码 #
8.1 字符编码 #
elixir
iex(1)> ?A
65
iex(2)> ?中
20013
iex(3)> <<20013::utf8>>
"中"
8.2 编码转换 #
elixir
iex(1)> :unicode.characters_to_binary("Hello", :utf8)
"Hello"
iex(2)> :unicode.characters_to_list("Hello", :utf8)
[72, 101, 108, 108, 111]
九、总结 #
本章学习了:
| 类型 | 语法 | 说明 |
|---|---|---|
| 字符串 | "Hello" |
UTF-8二进制 |
| 字符列表 | 'Hello' |
字符编码列表 |
| 二进制 | <<1, 2, 3>> |
字节序列 |
| Sigil | ~s(), ~c(), ~r/ |
特殊语法糖 |
准备好学习模式匹配了吗?让我们进入下一章。
最后更新:2026-03-27