Compare javascript strings with  

来源:互联网 发布:中国外债数据 编辑:程序博客网 时间:2024/05/16 10:00

/*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:

<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 ordervar 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 "&nbsp";


I use the unicode of "&nbsp", and it works!!!! It's strange, but at least it works for me now !!!!

stringA = stringA.replace("/\u00a0/g", "");

Enjoy coding !!!


原创粉丝点击