在 Julia 中将字符串拆分为子字符串数组 – split() 和 rsplit() 方法
split()
是 julia 中的内置函数,用于在出现指定分隔符时将指定字符串拆分为子字符串数组。
Syntax:
split(str::AbstractString, dlm; limit::Integer, keepempty::Bool)
or
split(str::AbstractString; limit::Integer, keepempty::Bool)
Parameters:
- str::AbstractString: Specified string.
- Dlm: Specified delimiter.
Below arguments are optional:
- limit::Integer: It is the maximum size of the result. It’s default value is zero (0).
- keepempty::Bool: It says whether the empty fields should be kept in the result or not. Its Default value is false.
Returns: It returns an array of substrings of the specified string.
例子:
# Julia program to illustrate
# the use of String split() method
# Splitting of the specified strings
Println(split("Gee.ks", "."))
Println(split("Geeks, for, Geeks", ", "))
Println(split("GFG-gfg", "-", limit = 1))
Println(split("GFG-gfg", "-", limit = 2))
输出:
字符串 rsplit() 方法
rsplit()
方法的工作方式与split()
方法完全相同,但从字符串的末尾开始。
Syntax:
rsplit(s::AbstractString; limit::Integer, keepempty::Bool)
or
rsplit(s::AbstractString, chars; limit::Integer, keepempty::Bool)
Parameters:
- str::AbstractString: Specified string.
- chars: Specified characters.
Below arguments are optional:
- limit::Integer: It is the maximum size of the result. It’s default value is zero (0).
- keepempty::Bool: It says whether the empty fields should be kept in the result or not. Its Default value is false.
Returns: It returns an array of substrings of the specified string.
例子:
# Julia program to illustrate
# the use of String rsplit() method
# Splitting of the specified strings
Println(rsplit("Gee.ks", "."))
Println(rsplit("Geeks, for, Geeks", ", "))
Println(rsplit("GFG-gfg", "-"; limit = 1))
Println(rsplit("GFG-gfg", "-"; limit = 2))
Println(rsplit("G-F-G-g-f-g", "-"; limit = 4))
输出: