Is this a .net 4.0 bug

  • Thread starter Thread starter Jesper, Denmark
  • Start date Start date
J

Jesper, Denmark

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?

regards Jesper.
 
Jesper said:
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?

I don't see anything wrong and when I copied your code snippet into the
following code sample

using System;

namespace Test2010042601
{
public class Test
{
public static void Main()
{
Console.WriteLine("7 mpaa".IndexOf("mpa").ToString());
}
}
}

and compiled it with " Visual C# 2010 Compiler version 4.0.30319.1" and
run it it output "2" as expected.

So I can't reproduce the problem.
 
Jesper said:
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?

It writes 2 as expected.
If you post the complete code, maybe we can find out what *is* causing
your problem.
 
Jesper said:
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 "Ã¥"?
 
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>

Arne
 
Arne Vajhøj said:
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
 
Peter K said:
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 "Ã¥".

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
 
Back
Top