Why can't IndexOf find the character N in combination with Y in hungarian culture?
Asked Answered
C

1

20

The IndexOf function called on a string returns -1, while there definitely is a match.

string sUpperName = "PROGRAMOZÁSI NYELVEK II. ADA EA+GY. (BSC 08 A)";
string sUpperSearchValue = "N";

sUpperName.IndexOf(sUpperSearchValue); // Returns -1

sUpperSearchValue = "NY";
sUpperName.IndexOf(sUpperSearchValue); // Returns 13

sUpperName[13]; // 78 'N'
sUpperSearchValue[0]; // 78 'N'
sUpperName[13] == sUpperSearchValue[0]; // true

Do you have any idea, why it is that it finds "NY" but not "N" by itself? If I search for every other letter in the string, it is able to find it, but not the "N". The same issue appears as well with lower case.

If I type " N" no match either, at " NY" it does.

Picture of this in console here

Clarino answered 31/5, 2014 at 13:17 Comment(3)
Both print 13 here. Are you absolutely sure this is the code you use yourself? Can you copy the N from NY to sUpperSearchValue? (actual ctrl+c, ctrl+v).Sardius
Maybe in one case the N is from the latin alphabet and in the other case is from the Hungarian (just guessing here) alphabet. If this is the case, they are different characters and it explains this behaviourBergwall
But can that explain, why only one of the letters reproduces this issue, but the others don't?Clarino
V
22

In Hungarian .NET Culture the letter combination "ny" stands for a separate letter, so there is no "N" there, only "NY" in your example.

Ventris answered 31/5, 2014 at 13:24 Comment(4)
Changing my culture to HU-hu does indeed allow me to reproduce the problem; previously working code now returns -1 for N. Placing the N separate from Y works in hungarian as well; does this mean that N and Y can never be placed together without referring to Ny instead of N and Y separately?Sardius
@GrantWinney Yeah, those are different letters, both are available. Although in this case its heuristic is correct, this letter combination is the ny (in "nyelvek"), not two letters.Mammoth
Aye, that makes sense. Thank you for the fast reply.Clarino
@JeroenVannevel I am not sure, in Hungarian language these are seldom occurring adjacent not meaning the single letter, but you can always use an invariant Culture (msdn.microsoft.com/en-us/library/…)Mammoth

© 2022 - 2024 — McMap. All rights reserved.