在朱莉娅字符串连接是附加两个或多个字符串连接成一个字符串是否是逐个或使用一些特殊的字符端到端的方式。有很多方法可以执行字符串连接。
例子:
Input: str1 = 'Geeks'
str2 = 'for'
str3 = 'Geeks'
Output: 'GeeksforGeeks'
连接方法
我们可以在 Julia 中连接字符串的不同方式是:
- 使用*运算符
- 使用^运算符
- 使用字符串()函数
使用“*”运算符
它用于将不同的字符串和/或字符成一个字符串。我们可以使用*运算符在 Julia 中连接两个或多个字符串。
例子:
Julia
# creating string 1
s1 = "Hello "
# creating string 2
s2 = "World !"
# concatenating the strings
s = s1 * s2
# printing the concatenated string
print(s)
Julia
# creating string
s1 = "Hello "
# concatenating the string
s = s1 ^ 5
# printing the concatenated string
print(s)
Julia
# creating string 1
s1 = "Hello "
# creating string 2
s2 = "World !"
# concatenating the strings
s = string(s1, s2)
# printing the concatenated string
print(s)
Julia
# creating 3 strings
s1 = "I"
s2 = "Love"
s3 = "Julia"
# concatenating the strings
string(s1, " ", s2, " ", s3)
# printing the concatenated string
print(s)
Julia
# creating 3 strings
s1 = "I"
s2 = "Love"
s3 = "Julia"
# concatenating the strings
s = s1 * " " * s2 * " " * s3
# storing string s in a file
open("myfile.txt", "w") do io
write(io, s)
end;
输出:
使用“^”运算符
此运算符以指定的次数重复指定的字符串。当需要多次连接单个字符串时使用它。
例子:
朱莉娅
# creating string
s1 = "Hello "
# concatenating the string
s = s1 ^ 5
# printing the concatenated string
print(s)
输出:
使用字符串()函数
朱提供了字符串的连接的预定义字符串()函数。此函数将所有要连接的字符串作为参数,并返回单个连接的字符串。
string(string1, string2, string3, string4, …)
示例 1:
朱莉娅
# creating string 1
s1 = "Hello "
# creating string 2
s2 = "World !"
# concatenating the strings
s = string(s1, s2)
# printing the concatenated string
print(s)
输出:
示例 2:
朱莉娅
# creating 3 strings
s1 = "I"
s2 = "Love"
s3 = "Julia"
# concatenating the strings
string(s1, " ", s2, " ", s3)
# printing the concatenated string
print(s)
输出:
存储到文件
可以使用write()函数将连接的字符串存储在文件中。在这里,首先连接完成,然后文件首先打开到“w”即写模式,连接的字符串使用 write() 存储。
例子:
朱莉娅
# creating 3 strings
s1 = "I"
s2 = "Love"
s3 = "Julia"
# concatenating the strings
s = s1 * " " * s2 * " " * s3
# storing string s in a file
open("myfile.txt", "w") do io
write(io, s)
end;
输出: