From: Peter K on
"Arne Vajhøj" <arne(a)vajhoej.dk> wrote in message
news:4bd63641$0$277$14726298(a)news.sunsite.dk...
> On 26-04-2010 20:17, Peter K wrote:
>> "Jesper, Denmark" <JesperDenmark(a)discussions.microsoft.com> wrote in
>> message news:3766D835-023C-4A17-B12F-06E6F8E0F7D3(a)microsoft.com...
>>> Console.WriteLine("7 mpaa".IndexOf("mpa").ToString());
>>>
>>> The above line write -1 on my screen, I would expect the value of 2.
>>> What is going on?
>>
>> This is just a guess - could it be because your program is running in a
>> Danish locale? The letter combination "aa" is interpreted as the Danish
>> letter "å"?
>
> That is it.
>
> using System;
> using System.Globalization;
> using System.Threading;
>
> namespace E
> {
> public class Program
> {
> public static void Main(string[] args)
> {
> Console.WriteLine("7 mpaa".IndexOf("mpa"));
> Console.WriteLine("7 mpaa".IndexOf("mpa",
> StringComparison.InvariantCulture));
> Console.WriteLine("7 mpaa".IndexOf("mpa",
> StringComparison.Ordinal));
> Thread.CurrentThread.CurrentCulture =
> CultureInfo.InvariantCulture;
> Console.WriteLine("7 mpaa".IndexOf("mpa"));
> Console.ReadKey(true);
> }
> }
> }
>
> outputs:
>
> da-DK
> -1
> 2
> 2
> 2
>
> here.
>
> The docs also state:
>
> <quote>
> Remarks
>
> Index numbering starts from zero.
>
> This method performs a word (case-sensitive and culture-sensitive) search
> using the current culture. The search begins at the first character
> position of this instance and continues until the last character position.
>
> Notes to Callers
>
> As explained in Best Practices for Using Strings in the .NET Framework, we
> recommend that you avoid calling string comparison methods that substitute
> default values and instead call methods that require parameters to be
> explicitly specified. To find the first index of a substring within a
> string instance by using the comparison rules of the current culture, call
> the IndexOf(String, StringComparison) method overload with a value of
> StringComparison::CurrentCulture for its comparisonType parameter.
> </quote>

And apparently you have to be careful the following:

string str = "7 mpaa";

int length = str.Length;
int index1 = str.IndexOf("mpa"); // -1
int index2 = str.IndexOf("mpaa"); // 2
int index3 = str.IndexOf("mpå"); // -1

The value of index1 tells us there is no "mpa" (because it's using Danish
culture, so "aa" is considered "å"). But index3 tells us that "aa" is not
the same as "å".

/Peter

From: Finn Stampe Mikkelsen on
"Peter K" <peter(a)parcelvej.dk> skrev i meddelelsen
news:#abLhyc5KHA.1924(a)TK2MSFTNGP06.phx.gbl...
> "Arne Vajhøj" <arne(a)vajhoej.dk> wrote in message
>>
>> As explained in Best Practices for Using Strings in the .NET Framework,
>> we recommend that you avoid calling string comparison methods that
>> substitute default values and instead call methods that require
>> parameters to be explicitly specified. To find the first index of a
>> substring within a string instance by using the comparison rules of the
>> current culture, call the IndexOf(String, StringComparison) method
>> overload with a value of StringComparison::CurrentCulture for its
>> comparisonType parameter.
>> </quote>
>
> And apparently you have to be careful the following:
>
> string str = "7 mpaa";
>
> int length = str.Length;
> int index1 = str.IndexOf("mpa"); // -1
> int index2 = str.IndexOf("mpaa"); // 2
> int index3 = str.IndexOf("mpå"); // -1
>
> The value of index1 tells us there is no "mpa" (because it's using Danish
> culture, so "aa" is considered "å").

------ >>> But index3 tells us that "aa" is not the same as "å".
>
> /Peter

Which actually is an correct interpretation from both you and the framework.
Although "aa" is considered to "å" in the danish locale and vocaly are
interchangeable, they are certianly not the same in written form. Although
"aa" is an old way of writing "å", "aa" is still represented in some names
of both people and places and it is considered misspelling, to substitute
"aa" with "å"....

Vestergaard and Vestergård are both names that are pronounces the same, but
spelled differently and you cannot choose yourself.

Why "mpa" is not found in "7 mpaa" is somewhat of a conundrum, but probably
a way of deal with the confusion. Either to interpret the "aa" as "å" or
simply 2 consecutive a's..

I would never be in doubt and do understand the OP's confusion, but i
understand the FW's interpretation of it and can se how impossible it would
be to take every eventuallity into account...

/Finn