4

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);
STF
  • 1,485
  • 3
  • 19
  • 36
CMartins
  • 3,247
  • 5
  • 38
  • 53

2 Answers2

3

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>
pepperjack
  • 673
  • 6
  • 20
  • can I change my substring and check both types? ( and ( ? – CMartins Nov 20 '17 at 15:46
  • @Carlos Martins take a look at https://stackoverflow.com/questions/273789/is-there-a-version-of-javascripts-string-indexof-that-allows-for-regular-expr – pepperjack Nov 20 '17 at 15:50
1

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);

YAJIE
  • 11
  • 2
  • I can see that in the Chinese version I don't have the white space so I don't need the -1 – CMartins Nov 20 '17 at 15:49
  • 1
    Yes, it is. The key thing here is just that you need to pay some attention to some special Chinese symbols when you deal with the Chinese strings. – YAJIE Nov 20 '17 at 15:54