Why doesn't this substring work in Chinese?

Viewed 780

Why doesn't this substring work in Chinese? I'm getting the correct results for other languages but in Chinese I got an empty string.

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

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

The ( and the chinese are different unicode characters. You need to use .indexOf('(') for chinese characters.

Example:

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

Because '(' is not in countryID, '(' and '(' are different characters, the first is chinese style bracket.

So maybe you can use:

countryID.substring(0, countryID.indexOf('(') - 1);

Related