localecompare_JavaScript中带示例的String localeCompare()方法

localecompare

JavaScript | 字符串localeCompare()方法 (JavaScript | String localeCompare() Method)

The String.localeCompare() method in JavaScript is used to compare two string in JavaScript. The result of the comparison will be,

JavaScript中的String.localeCompare()方法用于比较JavaScript中的两个字符串。 比较的结果将是

  • Positive: str1 is lexicographically greater than str2.

    正值 :在字典上str1str2大

  • Zero: str1 is equal to str2.

    str1等于str2

  • Negative: str1 is lexicographically smaller than str2.

    负数 :在字典上str1str2小。

Syntax:

句法:

    str1.localeCompare(str2);

Parameter(s):

参数:

The method accepts one parameter which is the string to be compared with the calling string.

该方法接受一个参数,该参数是要与调用字符串进行比较的字符串。

Return value:

返回值:

The return type of this method is number which is the result of the comparison.

此方法的返回类型是数字 ,它是比较的结果。

Browser support: Chrome, Internet explorer, Mozilla, Safari, Opera mini.

浏览器支持: Chrome,Internet Explorer,Mozilla,Safari,Opera mini。

Example 1:

范例1:

<script>
	document.write("Hello".localeCompare("Hello") + "<br/>");
	document.write("hello".localeCompare("hello") + "<br/>");
	document.write("Hello".localeCompare("hello") + "<br/>");
	document.write("hello".localeCompare("Hello") + "<br/>");
	document.write("ABC".localeCompare("BCD") + "<br/>");
	document.write("BCD".localeCompare("ABC") + "<br/>");
	document.write("PREM".localeCompare("SHIVANG") + "<br/>"); 
</script>

Output

输出量

0
0
1
-1
-1
1
-1

Example 2:

范例2:

<script>
    str1 = "include"
    str2 = "Help"
    
    result = str1.localeCompare(str2);
  
    if (result) {
        document.write(str1 + " is greater than " + str2 + "<br>");
    }
</script>

Output

输出量

include is greater than Help


翻译自: https://www.includehelp.com/code-snippets/string-localecompare-method-with-example-in-javascript.aspx

localecompare

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值