Engee 文档
Notebook

创建字符串数组

字符串数组可存储文本片段,并提供一系列函数来处理它们。与其他类型的数组一样,您可以对字符串数组进行索引、重塑和合并。本文将介绍一些处理字符串数组的函数。


字符串数组的每个元素都包含 1 到 n 的字符序列。

In [ ]:
str = "Hello, world"
Out[0]:
"Hello, world"

让我们使用 [] 操作符创建一个字符串矩阵。

In [ ]:
satellites = ["Ganymede" "Europa" "Callisto";"Amalthea" "Rings of Jupiter" "Leda"]
Out[0]:
2×3 Matrix{String}:
 "Ganymede"  "Europa"            "Callisto"
 "Amalthea"  "Rings of Jupiter"  "Leda"

字符串数组支持索引。让我们使用索引来访问字符串矩阵的第一行。

In [ ]:
satellites[1,:]
Out[0]:
3-element Vector{String}:
 "Ganymede"
 "Europa"
 "Callisto"

让我们访问 str 第二行中的第二个元素。

In [ ]:
satellites[2,2]
Out[0]:
"Rings of Jupiter"

您可以使用函数size() 来确定给定矩阵的大小。

In [ ]:
size(satellites)
Out[0]:
(2, 3)

使用函数length() 确定数组元素的数量。

In [ ]:
length(satellites)
Out[0]:
6

您还可以确定字符串数组中每个元素的字符数。如果我们在括号前加一个点。这意味着我们正在访问一个数组元素,并对其应用函数length()

In [ ]:
length.(satellites)
Out[0]:
2×3 Matrix{Int64}:
 8   6  8
 8  16  4

您可以使用函数string() 将一组数值转换为字符串。例如,让我们获取日期和时间并将其转换为字符串。

In [ ]:
using Dates

d = now()
string(d)
Out[0]:
"2024-05-20T12:28:05.120"

创建空字符串

字符串数组可以包含空值和缺失值。空字符串包含零字符。显示空字符串时,结果是一对双引号,其中没有任何字符("")。空字符串相当于数字数组中的 NaN 字符串。它表示字符串数组中缺失的值。显示空白字符串时,结果为missing

您可以使用函数String() 创建一个空字符串。

In [ ]:
str = String("")
Out[0]:
""

例如,您可以使用函数fill() 创建空行矩阵。

In [ ]:
str = fill("",(2,3))
Out[0]:
2×3 Matrix{String}:
 ""  ""  ""
 ""  ""  ""

要创建缺失行,需要为变量指定关键字 missing

In [ ]:
str = missing
Out[0]:
missing

您可以创建一个包含空字符串和缺失字符串的字符串数组。

In [ ]:
str = ["" "Ram" missing]
Out[0]:
1×3 Matrix{Union{Missing, String}}:
 ""  "Ram"  missing

使用函数ismissing() ,可以确定哪些元素是缺失值字符串。请注意,空字符串不是缺失字符串。

In [ ]:
ismissing.(str)
Out[0]:
1×3 BitMatrix:
 0  0  1

让我们使用函数occursin() 查找字符串中的空格字符,并使用函数replace() 将其替换为破折号。

In [ ]:
TF = occursin.(" ", satellites)
Out[0]:
2×3 BitMatrix:
 0  0  0
 0  1  0
In [ ]:
satellites = replace.(satellites, " " => "-")
display(satellites)
2×3 Matrix{String}:
 "Ganymede"  "Europa"            "Callisto"
 "Amalthea"  "Rings-of-Jupiter"  "Leda"

拆分、合并和排序字符串数组

将字符串合并到字符串数组中,就像合并其他类型的数组一样。

In [ ]:
str1 = ["a","b","c"];
str2 = ["d","e","f"];
str3 = ["g","h","i"];
str = [str1 str2 str3]
Out[0]:
3×3 Matrix{String}:
 "a"  "d"  "g"
 "b"  "e"  "h"
 "c"  "f"  "i"

permutedims() 函数允许您对矩阵的行元素进行转置。

In [ ]:
str = permutedims(str)
Out[0]:
3×3 Matrix{String}:
 "a"  "b"  "c"
 "d"  "e"  "f"
 "g"  "h"  "i"

要在行中添加文本,请使用 * 操作符。操作符会在行中添加文本,但不会改变行数组的大小。

In [ ]:
Name = ["Mary", "John", "Elizabeth", "Paul", "Ann"]
Name = [name * " Smith" for name in Name]
Out[0]:
5-element Vector{String}:
 "Mary Smith"
 "John Smith"
 "Elizabeth Smith"
 "Paul Smith"
 "Ann Smith"

例如,让我们合并一个包含姓和名的数组。

In [ ]:
Name = ["Mary", "John", "Elizabeth", "Paul", "Ann"];
Lastname = ["Jones", "Adams", "Young", "Burns", "Spencer"];
full_names = Name .* " " .* Lastname
Out[0]:
5-element Vector{String}:
 "Mary Jones"
 "John Adams"
 "Elizabeth Young"
 "Paul Burns"
 "Ann Spencer"

但还有一个 split 函数split() 。它可以用来分割数组中的字符串元素。

In [ ]:
full_names = split.(full_names)
Out[0]:
5-element Vector{Vector{SubString{String}}}:
 ["Mary", "Jones"]
 ["John", "Adams"]
 ["Elizabeth", "Young"]
 ["Paul", "Burns"]
 ["Ann", "Spencer"]

字符串元素的排序可以通过函数sort() 来完成。

In [ ]:
sort(Name)
Out[0]:
5-element Vector{String}:
 "Ann"
 "Elizabeth"
 "John"
 "Mary"
 "Paul"

除了上述处理字符串数组的示例外,还有许多其他函数。要了解有关功能的更多信息,请参阅 数组