GeeksforGeeks App
Open App
Browser
Continue

# Compare two strings in Julia – cmp() Method

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
```

My Personal Notes arrow_drop_up