How to compare two strings in the current locale with JavaScript?


In this tutorial, we will learn to compare two strings in a current locale with JavaScript. The meaning of the locale is local place or region. Here, we need to compare two strings based on the language of the particular locale.

Normally, when users compare the strings using equality or strict equality operators, it doesn’t compare strings based on the current locale. So, we will learn to compare the two strings based on the language of the particular locale.

Compare two strings using the localeCompare() Method

The String localeCompare() method compares the string with the reference string according to the browser's language, which means the current locale. It is an object method, so users need to invoke the method by using the reference string and passing the second string as a parameter.

Syntax

We can follow the syntax below to use the localeComapre() method.

str1.localeCompare(str2);

Parameters

  • str1 − It is a reference string, which is always required.

  • str2 − It is a parameter string; that users need to compare with the reference string.

Return values

  • 0 − If both the strings are equal.
  • 1 − If reference string is greater than parameter string.
  • -1 − If reference string is smaller than parameter string

Example

In the example below, we created the function comapreString(), which takes two strings as a parameter and renders the output after comparing both strings. Inside the function, we are using the localeCompare() method.

<html> <head> </head> <body> <h2> Compare two strings with current locale. </h2> <h4> Comparing strings using <i> localeCompare </i> method. </h4> <p id = "output"> </p> <script> let output = document.getElementById("output"); let str1 = "hello"; let str2 = "TutorialsPoint"; function compareStrings(str1, str2) { let result = str1.localeCompare(str2); if (result == 0) { output.innerHTML += str1 + " == " + str2 + "<br/>"; } else if (result == 1) { output.innerHTML += str1 + " > " + str2 + "<br/>"; } else { output.innerHTML += str1 + " < " + str2 + "<br/>"; } } compareStrings(str1, str2); compareStrings("abc", "abc"); </script> </body> </html>

In the above output, users can see that string “TutorialPoint” has higher alphabetical order than the string “hello.”

Compare Two Strings in a Different Locale

Compare Two Strings in a Different Locale In this approach, we will use the same method localeCompare() to match the string, but we will change the current locale and compares the string according to the different regional language. Also, we will pass some options as a parameter of the localeCompare() method to compare strings according to options.

Syntax

Users can follow the syntax below to use the different locales with the localeCompare() method.

let str1 = "'Hello programmers!'";
let str2 = "Welcome, here.";
let options = { ignorePunctuation: true }
let result = str1.localeCompare(str2, "en-US", options);

Parameters

  • en-US − It is a locale, based on the regional language of that locale users want to compare the two strings. It is an optional parameter. The en-US suggest the English language of the US region. We can add different a code for a different locale.

  • options − It is also an optional parameter, which contains different options to compare the string. In our case, it will ignore the punctuations, while comparing the strings.

Example

In the example below, we are comparing two strings based on the en-US locale. Also, we are ignoring the punctuations while comparing the strings.

<html> <head> </head> <body> <h2> Comparing two strings with current locale. </h2> <h4> comparing strings in different locale using <i>localeCompare</i> method. </h4> <p id = "output"> </p> <script> let output = document.getElementById("output"); let str1 = "'Hello programmers!'"; let str2 = "Welcome, here."; function compareStrings(str1, str2) { let options = { ignorePunctuation: true } let result = str1.localeCompare(str2, "en-US", options); if (result == 0) { output.innerHTML += str1 + " == " + str2 + "<br/>"; } else if (result == 1) { output.innerHTML += str1 + " > " + str2 + "<br/>"; } else { output.innerHTML += str1 + " < " + str2 + "<br/>"; } } compareStrings(str1, str2); </script> </body> </html>

We have learned to compare two strings based on the specified locale. If we do not specify the locale, it will take the browser's default language and compare the string basis on that. Also, users can use the Mathematical operators to compare two strings.

Updated on: 17-Aug-2022

455 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements