您的位置:首页 > Web前端 > JavaScript

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:

<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 !!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: