Why does this substring not work in Chinese?

Why does this substring not work in Chinese? I get 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);
+4
source share
2 answers

(and Chinese are different Unicode characters. You must use .indexOf('(')for Chinese characters.

Example:

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

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

So you can use:

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

+1
source

Source: https://habr.com/ru/post/1689579/


All Articles