📜  Julia 中的字符串操作

📅  最后修改于: 2022-05-13 01:55:47.588000             🧑  作者: Mango

Julia 中的字符串操作

字符串操作是分析和处理字符串以通过操作或更改字符串中存在的数据来达到预期结果的过程。 Julia 提供了各种字符串操作,我们可以将其应用于字符串来实现这一点。我们将进一步详细讨论此类操作。

首先,我们创建一个字符串来操作:

Julia
#  Create a string
s = "Everybody has an interesting geek in them"


Julia
#  find the index of occurrence of the letter 'd' in the string
i = findfirst(isequal('d'), s)


Julia
#  find the start and end indices of occurrence of the word 'geek'
r = findfirst("geek", s)


Julia
#  find the indices of characters with first letter 'i' and last letter 'g'
r = findfirst(r"i[\w]*g", s)


Julia
# replace the word geek with champian in the string
r = replace(s, "geek" => "champian")


Julia
# replace the character with first letter i and last letter g with the word adventurous
r = replace(s, r"i[\w]*g" => "adventurous")


Julia
# create three strings
s1 = "Geeks"
s2 = "For"
s3 = "Geeks"
  
# concatenate strings
string(s1, s2, s3)


Julia
# concatenate strings with characters in between
string(s1, ", ", s2, "- ", s3)


Julia
# concatenate three strings with *
s1*", "*s2*", "*s3


Julia
# display all characters in all of the three strings
collect.([s1, s2, s3])
 
# display length of the three strings
length.([s1, s2, s3])


Julia
# display element matching character
# with first letter g and last letter k
# with Regekmatch type
r = match(r"g[\w]*k", s)
 
# display the matched character
show(r.match); println()


Julia
# display characters with length
# equal to or greater than 4
r = eachmatch(r"[\w]{4, }", s)
 
for i in r println("\"$(i.match)\" ")
end


Julia
# display characters with length equal to or greater than 4
r = collect(m.match for m = eachmatch(r"[\w]{3, }", s))


Julia
# strip white spaces in the string
r = strip("Geek  ")
 
# strip white spaces and the letter G in the string
r = strip("Geek  ", ['G', ' '])


Julia
# split string on ', ' character
r = split("Geeks, for, Geeks", ', ')


Julia
# split string on ', ' character
r = split("Geeks, for, Geeks", ", ")


Julia
# eliminating whitespaces while splitting the string
r = split("Geeks, for, Geeks", [', ', ' '],
             limit = 0, keepempty = false)


Julia
# joining characters or strings with ', ' in between
r = join(collect(1:7), ", ")


操纵-01

搜索字符串中的元素

要在字符串中搜索一个或多个元素,我们可以使用findfirst()函数,它返回我们放置的字符的第一个索引,如下所示:

朱莉娅

#  find the index of occurrence of the letter 'd' in the string
i = findfirst(isequal('d'), s)

操纵-02

我们可以在字符串中放置一个返回该单词的开始和结束索引的整个单词:

朱莉娅

#  find the start and end indices of occurrence of the word 'geek'
r = findfirst("geek", s)

操纵-03

我们还可以通过指定第一个和最后一个字母来搜索一个单词:

朱莉娅

#  find the indices of characters with first letter 'i' and last letter 'g'
r = findfirst(r"i[\w]*g", s)

替换字符串的元素

要将字符串中的元素或字符替换为其他元素,可以通过以相同方式指定要替换的字符来使用replace()函数。

朱莉娅

# replace the word geek with champian in the string
r = replace(s, "geek" => "champian")

朱莉娅

# replace the character with first letter i and last letter g with the word adventurous
r = replace(s, r"i[\w]*g" => "adventurous")

字符串的连接

串联是将事物串联在一起的操作。它是最有用的字符串操作之一,Julia 提供了一种非常简单的方法来实现它。

朱莉娅

# create three strings
s1 = "Geeks"
s2 = "For"
s3 = "Geeks"
  
# concatenate strings
string(s1, s2, s3)

我们也可以在字符串之间放置任何字符:

朱莉娅

# concatenate strings with characters in between
string(s1, ", ", s2, "- ", s3)

使用“ *”运算符 对于直接字符串连接:

朱莉娅

# concatenate three strings with *
s1*", "*s2*", "*s3

获取字符串的大小

collect()length()函数可分别用于获取多个字符串的所有字符和长度:

朱莉娅

# display all characters in all of the three strings
collect.([s1, s2, s3])
 
# display length of the three strings
length.([s1, s2, s3])

执行匹配操作

Julia 允许我们使用match()函数从左到右扫描字符串以查找返回 RegexMatch 类型的第一个匹配项(指定的起始索引可选):

朱莉娅

# display element matching character
# with first letter g and last letter k
# with Regekmatch type
r = match(r"g[\w]*k", s)
 
# display the matched character
show(r.match); println()

我们可以使用eachmatch()函数对字符串进行迭代。以下示例显示了具有指定长度的匹配元素的实现。

朱莉娅

# display characters with length
# equal to or greater than 4
r = eachmatch(r"[\w]{4, }", s)
 
for i in r println("\"$(i.match)\" ")
end

朱莉娅

# display characters with length equal to or greater than 4
r = collect(m.match for m = eachmatch(r"[\w]{3, }", s))

剥离、拆分和连接操作

strip()函数用于消除字符串中的某些字符。如果没有提到我们要在函数中消除的字符, strip()函数会消除字符串中的空格。我们可以通过将它们作为第二个参数放置在strip()函数中来提及我们想要在字符串中消除的字符,如下例所示:

朱莉娅

# strip white spaces in the string
r = strip("Geek  ")
 
# strip white spaces and the letter G in the string
r = strip("Geek  ", ['G', ' '])

split()函数可用于在特定字符上拆分字符串:

朱莉娅

# split string on ', ' character
r = split("Geeks, for, Geeks", ', ')

朱莉娅

# split string on ', ' character
r = split("Geeks, for, Geeks", ", ")

我们还可以在删除以下示例中表示的字符时进行拆分:

朱莉娅

# eliminating whitespaces while splitting the string
r = split("Geeks, for, Geeks", [', ', ' '],
             limit = 0, keepempty = false)

join()函数是 split 的反面,我们可以使用它来连接字符串之间的特定字符。

朱莉娅

# joining characters or strings with ', ' in between
r = join(collect(1:7), ", ")