Compare javascript strings with  
2011-12-10 05:28
405 查看
/*By Jiangong SUN*/
Today I wanted to compare two string in javascript, they both have the same values but they are not equal when i compare them.
Here is my html code:
My js code :
Finally, my code doesn't work 'cause it always return -1, but not 0.
After struggling during hours, I thought it's the problem of " ";
I use the unicode of " ", and it works!!!! It's strange, but at least it works for me now !!!!
Enjoy coding !!!
Today I wanted to compare two string in javascript, they both have the same values but they are not equal when i compare them.
Here is my html code:
<div class='A'> <span>3</span> <span>6</span> <span>-&-n-b-s-p-;</span> <span>7</span> <span>5</span> <span>9</span> <span>,</span> <span>0</span> <span>0</span> <span>€</span> </div> <div class='B'> 36 759,00 € </div>
My js code :
var stringA = ''; $.each($('.A'), function(){ stringA += $(this).text(); }); stringA = $.trim(stringA).replace(" ", ""); var stringB = $.trim($('.B').text()).replace(" ", ""); //localeCompare is a method for compare string in javascript //0 : It string matches 100%. //-1 : no match, and the parameter value comes before the string object's value in the locale sort order //1 : no match, and the parameter value comes after the string object's value in the locale sort order var equals = stringA.localeCompare(stringB);
Finally, my code doesn't work 'cause it always return -1, but not 0.
After struggling during hours, I thought it's the problem of " ";
I use the unicode of " ", and it works!!!! It's strange, but at least it works for me now !!!!
stringA = stringA.replace("/\u00a0/g", "");
Enjoy coding !!!
相关文章推荐
- JavaScript 将html中空格( )转成JavaScript中的空格
- javascript 在文本框中…
- 【JavaScript】Ajax 中如何上传文…
- HDU 2578 Dating with girls(1)
- JavaScript&nbsp;的学习程序整理(…
- lightoj 1052 - String Growth & uva 12045 - Fun with Strings 矩阵
- javaScript——优化&nbsp;&nbsp;
- oracle&nbsp;with&nbsp;check&nbsp;option
- JavaScript 实现 复选框…
- JavaScript escape() 函数
- javascript 禁止输入、禁止粘…
- javascript 动态调用方法和传参数
- 安卓自定义标题时候you cannot combine custom titles with other 
- javascript&nbsp;对象类型
- [转]javaScript 数值型和字符串型…
- Tip/Trick: ASP.NET 2.0 Deal with DBNull value when bind to RadioButtonList/当绑定RadioButtonL
- ASP.NET 2.0 Language Swithcer and Theme Swicher 多语言转换和多样式主题转换
- ASP.NET 2.0: URL Mapping with RegEx Support
- resource is out of sync with t…
- android使用自定义标题时提示“you cannot combine custom titles with other  ”