2026a
# 文本和字符
# 字符串数组中的文本
当您处理文本时,将字符序列括在双引号中。可以将文本赋给变量。
t = "Hello, world";
如果文本包含双引号,请在定义中使用转义字符 \ 。
q = "Something \"quoted\" and something else."
"Something \"quoted\" and something else."
与所有 Syslab 变量一样,t 和 q 是数组。它们的数据类型是 String。
varinfo(r"^t")
name size summary
–––– –––––––– ––––––––––––––––––
t 20 bytes 12-codeunit String
要将文本添加到字符串末尾,请使用加号运算符 *。
f = 71;
c = (f-32)/1.8;
tempText = "Temperature is " * string(c) * "C"
"Temperature is 21.666666666666668C"
与数值数组类似,字符串数组可以有多个元素。使用 length 函数可查找数组中每个字符串的长度。
A = ["a" "bb" "ccc"; "dddd" "eeeeee" "fffffff"]
2×3 Matrix{String}:
"a" "bb" "ccc"
"dddd" "eeeeee" "fffffff"
length.(A)
2×3 Matrix{Int64}:
1 2 3
4 6 7
# 字符数组中的数据
有时字符表示的数据与文本不符,例如 DNA 序列。您可以将此类数据存储在数据类型为 char 的字符数组中。字符数组使用单引号。
seq = "GCTAGAATCC"
"GCTAGAATCC"
# 字符串与字符数组的转换
通过 collect 函数将字符串转成字符数组。
seq = collect(seq)
10-element Vector{Char}:
'G': ASCII/Unicode U+0047 (category Lu: Letter, uppercase)
'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
'T': ASCII/Unicode U+0054 (category Lu: Letter, uppercase)
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
'G': ASCII/Unicode U+0047 (category Lu: Letter, uppercase)
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
'T': ASCII/Unicode U+0054 (category Lu: Letter, uppercase)
'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
varinfo(r"^seq")
name size summary
–––– –––––––– –––––––––––––––––––––––
seq 80 bytes 10-element Vector{Char}
数组的每个元素包含一个字符。
seq[4]
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
使用 vcat 连接字符数组,就像串联数值数组一样。
seq2 = vcat(seq, collect("ATTAGAAACC"))
20-element Vector{Char}:
'G': ASCII/Unicode U+0047 (category Lu: Letter, uppercase)
'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
'T': ASCII/Unicode U+0054 (category Lu: Letter, uppercase)
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
'G': ASCII/Unicode U+0047 (category Lu: Letter, uppercase)
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
'T': ASCII/Unicode U+0054 (category Lu: Letter, uppercase)
'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
'T': ASCII/Unicode U+0054 (category Lu: Letter, uppercase)
'T': ASCII/Unicode U+0054 (category Lu: Letter, uppercase)
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
'G': ASCII/Unicode U+0047 (category Lu: Letter, uppercase)
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
通过 String 函数将字符数组转成字符串。
seq = String(seq2)
"GCTAGAATCCATTAGAAACC"
# 字符
# ASCII 字符 和 Unicode 字符
ch = 'a'
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
ch = '中'
'中': Unicode U+4E2D (category Lo: Letter, other)
ch == '\U4E2D'
true
# 字符类型转换
使用 UInt32 将字符转换成无符号 32 位整数值。
d = UInt32('中')
0x00004e2d
# 有效性判断
使用 isvalid 函数判断字符是否有效。
@assert isvalid(ch)
@assert isvalid(Char(0x00004e2d))
@assert isvalid(Char(0x11ffff)) == false # 0x11ffff 超出了 Unicode 编码标准所定义的代码空间
# 字符串与索引
str = "中国"
length(str) == 2
sizeof(str) == 6 # 对于 UTF-8 编码格式来说,一个中文字符需要用掉 3 个代码单元
str[1] == '中'
i = nextind(str, 1) # i等于4
str[i] == '国'
注意
str[1]: '中'
str[2]: 报错
str[3]: 报错
str[4]: '国'
# 使用迭代器
for ch in str
println(ch)
end
中
国