比较 Julia 中的两个字符串– cmp() 方法
cmp()
是 julia 中的内置函数,如果两个指定的字符串具有相同的长度并且每个索引处的字符在两个字符串中相同,则用于返回 0,如果 a 是 b 的前缀,则返回 -1,或者如果 a 按字母顺序在 b 之前,如果 b 是 a 的前缀,或者如果 b 按字母顺序在 a 之前,则返回 1。
Syntax:
cmp(a::AbstractString, b::AbstractString)
Parameters:
- a::AbstractString: Specified first string
- b::AbstractString: Specified second string
Returns: It returns 0 if the both specified strings are having the same length and the character at each index is the same in both strings, return -1 if a is a prefix of b, or if a comes before b in alphabetical order and return 1 if b is a prefix of a, or if b comes before a in alphabetical order.
示例 1:
# Julia program to illustrate
# the use of String cmp() method
# Comparing two strings and
# getting the values 0, -1 or 1
println(cmp("abc", "abc"))
println(cmp("a", "b"))
println(cmp("c", "b"))
println(cmp("ab", "abc"))
println(cmp("abc", "ab"))
println(cmp("ab", "ac"))
输出:
0
-1
1
-1
1
-1
示例 2:
# Julia program to illustrate
# the use of String cmp() method
# Comparing two strings and
# getting the values 0, -1 or 1
println(cmp("1", "2"))
println(cmp("1", "1"))
println(cmp("12", "21"))
println(cmp("123", "23"))
println(cmp("31", "23"))
输出:
-1
0
-1
-1
1