Elixir & Phoenix: Scalable Backend Development · 课时

Elixir 中的字符串、二进制数据与 sigil

探索 Elixir 如何将文本表示为 UTF-8 二进制数据,并使用 sigil 简洁地编写字符串、列表和正则表达式。

第 4 / 4 课13 个步骤

Elixir 中的字符串、二进制数据与 sigil 是 CoddyKit 上的免费 Elixir & Phoenix: Scalable Backend Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elixir & Phoenix: Scalable Backend Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Strings Are UTF-8 Binaries

An Elixir string is a UTF-8 binary in double quotes — not a list of characters. Keep that distinction in mind.

name = "Elixir"
IO.puts(name)

String Concatenation

Join two strings with <>, which concatenates the underlying binaries.

greeting = "Hello, " <> "World"
IO.puts(greeting)

String Interpolation

Drop expressions into a string with interpolation: the code is evaluated and its result slotted in.

name = "Ada"
IO.puts("Hello, #{name}!")

Common String Functions

The String module is full of helpers for transforming text, like upcase and length.

IO.puts(String.upcase("elixir"))
IO.puts(String.length("hello"))

Splitting and Trimming

Use String.split to break text into parts and String.trim to strip surrounding whitespace.

parts = String.split("a,b,c", ",")
IO.inspect(parts)
IO.puts(String.trim("  hi  "))

Strings vs Charlists

Single quotes make a charlist — a list of integers — not a string. Most modern APIs expect double-quoted strings.

IO.inspect('abc')
IO.inspect("abc")

Introducing Sigils

A sigil starts with ~ and a letter for compact literals. For example, ~s builds a string.

text = ~s(He said "hi")
IO.puts(text)

Word List Sigils

The ~w sigil builds a word list from whitespace-separated text — no noisy quotes or commas.

words = ~w(apple banana cherry)
IO.inspect(words)

Atom Word Lists

Append the a modifier to ~w and you get a list of atoms instead of strings.

keys = ~w(name age email)a
IO.inspect(keys)

Regex Sigils

The ~r sigil creates a regex, which you pair with the Regex module to match and replace.

IO.inspect(Regex.run(~r/\d+/, "abc123"))

Heredocs for Multiline Text

Triple-quoted heredocs hold multi-line text and double as documentation strings.

msg = """
Line one
Line two
"""
IO.puts(msg)

Quick Check

Test your Elixir string knowledge.

Recap

Recap: strings are UTF-8 binaries you join with <> and interpolate; the String module transforms them; charlists differ; and sigils keep literals concise.

免费开始

用 AI 导师学习 Elixir — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「Elixir 中的字符串、二进制数据与 sigil」课时是免费的吗?

是的 — 「Elixir 中的字符串、二进制数据与 sigil」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elixir & Phoenix: Scalable Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。

「Elixir 中的字符串、二进制数据与 sigil」这节课中我会学到什么?

探索 Elixir 如何将文本表示为 UTF-8 二进制数据,并使用 sigil 简洁地编写字符串、列表和正则表达式。 你通过在浏览器中直接运行的动手代码来练习 Elixir & Phoenix: Scalable Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Elixir & Phoenix: Scalable Backend Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Elixir & Phoenix: Scalable Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「Elixir 中的字符串、二进制数据与 sigil」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Elixir & Phoenix: Scalable Backend Development 课中编写并运行代码吗?

能。每节 Elixir & Phoenix: Scalable Backend Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Elixir 语言简介
  2. 基本数据类型与运算符
  3. 模式匹配与控制流
  4. Elixir 中的字符串、二进制数据与 sigil
← 返回 Elixir & Phoenix: Scalable Backend Development