比较JavaScript中的两个字符串

编程入门 行业动态 更新时间:2024-10-07 18:25:14
本文介绍了比较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.

推荐答案

www.java2s/Tutorial/JavaScript/0120__String/Comparetwostrings.htm [ ^ ] www.zimbio/Web+Design/articles/472/Javascript + Compare + Two + Input + Strings [ ^ ] javascript.about/od/decisionmaking/a/des02.htm [ ^ ] www.java2s/Tutorial/JavaScript/0120__String/Comparetwostrings.htm[^] www.zimbio/Web+Design/articles/472/Javascript+Compare+Two+Input+Strings[^] javascript.about/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中的两个字符串

本文发布于:2023-11-30 13:35:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650171.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   两个   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!