欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaScript中操作字符串之localeCompare()方法的使用

程序员文章站 2023-12-28 12:19:16
 这个方法返回一个数字表示参考字符串是否到来之前或之后或相同的排序顺序给定的字符串。 语法 string.localecompare( param )...

 这个方法返回一个数字表示参考字符串是否到来之前或之后或相同的排序顺序给定的字符串。
语法

string.localecompare( param )

下面是参数的详细信息:

  •     param : 字符串对象进行比较的字符串

返回值:

  •     0 : 字符串匹配100%
  •     1 : 不匹配,参数值来自于语言环境的排序顺序字符串对象的值之前
  •     -1 : 不匹配,参数值来自于语言环境的排序顺序字符串对象的值之后

例子:

<html>
<head>
<title>javascript string localecompare() method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new string( "this is beautiful string" );
var index = str1.localecompare( "xyz" );
document.write("localecompare first :" + index ); 

document.write("<br />" ); 

var index = str1.localecompare( "abcd ?" );
document.write("localecompare second :" + index ); 

</script>
</body>
</html>

这将产生以下结果:

localecompare first :-1
localecompare second :1 

上一篇:

下一篇: