为什么这个子字符串在中文中不起作用?
Why doesn't this substring work in Chinese?
为什么这个子字符串在中文中不起作用?
我得到了其他语言的正确结果,但在中文中我得到了一个空字符串。
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>
因为'('不在countryID
中,'('和'('是不同的字符,第一个是中式括号。
也许你可以使用:
countryID.substring(0, countryID.indexOf('(') - 1);
为什么这个子字符串在中文中不起作用? 我得到了其他语言的正确结果,但在中文中我得到了一个空字符串。
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>
因为'('不在countryID
中,'('和'('是不同的字符,第一个是中式括号。
也许你可以使用:
countryID.substring(0, countryID.indexOf('(') - 1);