Open In App

Compare two strings in Julia – cmp() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The cmp() is an inbuilt function in julia which is used to return 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.

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.

Example 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"))


Output:

0
-1
1
-1
1
-1

Example 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"))


Output:

-1
0
-1
-1
1


Last Updated : 26 Mar, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads