不同语言字符串对比
表格
普通 escape string 为 0-0-0;format string、raw string、多行字符串分别在三个位置设为 1,例如 0-1-1 表示多行 raw string。escape string、raw string、format string 分别简写为 estr、rstr、fstr。本文中,fstr 特指支持表达式或参数插值的字符串构造方式,它可能是字面量语法,也可能是常用的宏/库调用。
值得注意的是,该分类指“是否具有性质”,例如 0-0-0(单行)表示这种方法只能用于单行而绝不能用于多行;而 0-0-1(多行)表示这种方法可以同时用于单行或多行。类似地,对于既可以多行又是 rstr 的表示方法,它只能分配在 0-1-1 中,而不能分配在 0-0-1(多行)/ 0-1-0(rstr)中。大家不要产生“这个语言明明可以单行但是为什么单行为空 or 明明有多行字符串但为什么多行是 N/A”的误解。
另外,多行不考虑形如
s = "123, \
456"
的形式,因为这种语法(不管是 C++ 的全局适用还是 Rust 在字符串中将换行视为空字符(更况且 Rust 本来就支持换行))需要在每一行额外添加内容,不属于“把回车纳入字符串”这一性质。
| 语言 | estr | 多行 estr | rstr | 多行 rstr | fstr | 多行 fstr | fstr 插值语法 | frstr | 多行 frstr | 注释 |
|---|---|---|---|---|---|---|---|---|---|---|
0-0-0 |
0-0-1 |
0-1-0 |
0-1-1 |
1-0-0 |
1-0-1 |
1-1-0 |
1-1-1 |
|||
| Bash | N/A | $'...' |
N/A | '...' |
N/A | "..." |
$parameter、${parameter-expansion}、$((arithmetic-expression))、$(command) 与弃用中的 `command` |
N/A | N/A | '...' 内不能包含单引号,即使写成 \' 也不行。$'...' 中可以使用 \' 和 \";"..." 中双引号需要写成 \",单引号可以直接写,\' 不会作为转义处理。 |
| Python | "..." / '...' |
"""...""" / '''...''' |
r"..." (or r'...', 后略) |
r"""...""" |
f"..." |
f"""...""" |
{expression} |
fr"..." (or rf, 后略) |
fr"""...""" |
|
| TypeScript | "..." / '...' |
N/A | N/A | N/A | N/A | `...` |
${expression} |
N/A | String.raw`...` (ECMAScript 的标签函数) |
|
| Rust | N/A | "..." |
N/A | r#"..."# (# 可以没有或有至多 255 个,后略) |
N/A | format!("...") (format! 宏,格式语法见 std::fmt,后略) |
format!("...{}...", expression) |
N/A | format!(r#"..."#) |
单引号表示 char |
| C++ | "..." |
N/A | N/A | R"d-seq(...)d-seq" (d-seq 可以为空或有至多 16 个字符,后略) |
std::format("...") (在 <format> 中,后略) |
N/A | std::format("...{}...", expression) |
N/A | std::format(R"d-seq(...)d-seq") |
单引号表示 char |
| Java | "..." |
"""...""" (开头分隔符后必须紧接换行,后略) |
N/A | N/A | String.format("...") |
String.format("""...""") |
String.format("...%s...", expression) |
N/A | N/A | 单引号表示 char |
| C# | "..." |
N/A | N/A | @"..." / """...""" (" 可以有至少 3 个,后略) |
$"..." |
N/A | {expression} |
N/A | $@"..." / @$"..." / $"""...""" (仅 $"""...""" 的 $ 可以有多个,影响插值大括号个数,例如 $$$ 则插值语法为 {{{expression}}}) |
单引号表示 char |
| JSON | "..." |
N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | 不支持单引号 |
| YAML | N/A | "..." (换行后有字符折叠为空格,换行且空行保留为换行) |
N/A | '...'、> (前俩都是换行后有字符折叠为空格,换行且空行保留为换行)、| |
N/A | N/A | N/A | N/A | N/A | |
| TOML | "..." |
"""...""" |
'...' |
'''...''' |
N/A | N/A | N/A | N/A | N/A |
部分参考资料
Bash
- https://www.gnu.org/software/bash/manual/bash.html#Quoting-1
- https://www.gnu.org/software/bash/manual/bash.html#Shell-Expansions
Python
- https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str
- https://docs.python.org/3/tutorial/inputoutput.html#fancier-output-formatting
Java
- https://docs.oracle.com/en/java/javase/26/language/text-blocks.html
- https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/String.html#format(java.lang.String,java.lang.Object...)
C#
- https://learn.microsoft.com/dotnet/csharp/programming-guide/strings/
- https://learn.microsoft.com/dotnet/csharp/language-reference/tokens/interpolated
YAML
TOML