From: Jesper, Denmark on
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.
From: Martin Honnen on
Jesper wrote:
> 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.

--

Martin Honnen --- MVP Data Platform Development
http://msmvps.com/blogs/martin_honnen/
From: Willem van Rumpt on
Jesper wrote:
> 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.

--
Willem van Rumpt
From: Peter K on
"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 "å"?

From: Arne Vajhøj on
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>

Arne