编程语言
首页 > 编程语言> > javascript – 为什么这个子串不能用中文?

javascript – 为什么这个子串不能用中文?

作者:互联网

为什么这个子串不能用中文?
我得到了其他语言的正确结果,但在中文中我得到一个空字符串.

    countryID = "奥地利(销售超过3万5千欧元)";
    countryID.substring(0, countryID.indexOf('(') - 1);

    countryID = "Austria (Sales > €35,000)";
    countryID.substring(0, countryID.indexOf('(') - 1);

解决方法:

(和中文(是不同的unicode字符.你需要使用.indexOf(‘(‘)表示中文字符.

例:

<div id='d1'></div>
<script>
    var countryID = "奥地利(销售超过3万5千欧元)";
    document.getElementById('d1').innerHTML=countryID.indexOf('(');
</script>

标签:jquery,javascript,substring,chinese-locale
来源: https://codeday.me/bug/20190607/1195685.html