", "<", "==", and "===" operators to compare strings; 2. Use the localeCompare() method of strings to compare strings according to the locale. Agreed order to compare the size of two strings."/> ", "<", "==", and "===" operators to compare strings; 2. Use the localeCompare() method of strings to compare strings according to the locale. Agreed order to compare the size of two strings.">

Home >Web Front-end >JS Tutorial >What are the JavaScript string comparison methods?

What are the JavaScript string comparison methods?

青灯夜游
青灯夜游Original
2021-06-17 16:58:1117571browse
<blockquote> <p>JavaScript string comparison method: 1. Directly use ">", "594023529e5d730023f5f05702711494, <code><</code>, <code> ==</code>, <code>===</code> to compare the size of two strings, just like comparing two numbers. </p> <p>For example, the encoding of the lowercase letter a is 97, and the encoding of the uppercase letter A is 65, then the character "a" is greater than "A". </p><pre class="brush:php;toolbar:false">console.log("a" > "A"); //返回true</pre><p>For another example, the Unicode encoding of "Chinese" is \u4e2d\u56fd\u4eba, and the encoding of "Programming Language" is \u7f16\u7a0b\u8bed\u8a00, because \u4e2d is smaller than \u7f16, so " "Chinese" is smaller than "programming language". </p><pre class="brush:php;toolbar:false">console.log("中国人"<"编程语言"); //返回true</pre><p><strong>Use the localeCompare() method</strong></p> <p>Use the localeCompare() method of strings to compare the sizes of two strings according to the local convention order. The ECMAScript standard does not specify how to perform localized comparison operations. </p> <p>The localeCompare() method contains a parameter specifying the target string to be compared. If the current string is less than the parameter string, it returns a number less than 0; if it is greater than the parameter string, it returns a number greater than 0; if the two strings are equal, or there is no difference from the local sorting convention, the method returns 0. </p> <p>[Example] The following code converts the string "JavaScript" into an array, and then sorts it in local character order. </p><pre class="brush:php;toolbar:false">var s = "JavaScript"; //定义字符串直接量 var a = s.split(""); //把字符串转换为数组 var s1 = a.sort(function (a, b)) { //对数组进行排序 return a.localeCompare(b); //将根据前后字符在本地的约定进行排序 }); a = s1.join(""); //然后再把数组还原为字符串 console.log(a); //返回字符串“aaciJprStv”</pre><p>【Related recommendations: <a href="https://www.php.cn/course/list/17.html" target="_blank">javascript learning tutorial</a><strong>】</strong></p> </blockquote>

The above is the detailed content of What are the JavaScript string comparison methods?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn