比较JavaScript中的两个字符串 [英] compare two string in javascript

查看:78
本文介绍了比较JavaScript中的两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi ..
我正在尝试比较两个字符串以进行排序.我正在通过使用=,>直接比较此字符串.和< operator.i在某些情况下出现问题,例如:

if(abc12<abc2)
>它返回true,但实际我想要abc12字符串必须更大.

我的代码是

hi..
I am trying to compare two string for sorting purpose.i am comparing this string directly by using =,> and < operator.i am getting problem in some case eg:

if(abc12<abc2)
> it returns true but actual i want abc12 string must be greater.

my code is

str1=abc12;
str2=abc2;

if(str1<str2)
{
 alert('str1');
}
else
{
alert('str2');
}



我得到的输出为str1.其实我要str2.
提供解决方案.



i got output as str1. Actually i want str2.
Provide a solution.

推荐答案

http://www.java2s.com/Tutorial/JavaScript/0120__String/Comparetwostrings.htm [ http://www.zimbio.com/Web+Design/articles/472/Javascript + Compare + Two + Input + Strings [ ^ ]
http://javascript.about.com/od/decisionmaking/a/des02.htm [ ^ ]
http://www.java2s.com/Tutorial/JavaScript/0120__String/Comparetwostrings.htm[^]
http://www.zimbio.com/Web+Design/articles/472/Javascript+Compare+Two+Input+Strings[^]
http://javascript.about.com/od/decisionmaking/a/des02.htm[^]


首先,您应该查看函数 string.localCompare [
first of all you should look at the function string.localCompare[^] as well as to make it work properly you will have to compare string length too


hiii,

hiii,

function myFunction()
{
var x="abc123";
var y="abc122"
if((x.localeCompare(y)<0 && x.length<=y.length))
{
alert(y);
}
else
{
alert(x);
}
}




希望它能正常工作




hope this will work fine


这篇关于比较JavaScript中的两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆