strange behaviour with indexOf and single length strings

  • Thread starter Thread starter Shukri Adams
  • Start date Start date
S

Shukri Adams

I'm getting a weird error with IndexOf. It only occurs if
the substring length is 1.


"aaa".IndexOf("a")
for example should return 0 , ie, it should find the
first "a" in "aaa". Instead, it return 2, the last "a".
This error does not occur in

"aaaa".IndexOf("aa")
however. Here you get the expected result of 0. In other
words, it only occurs when the substring length is 1.
But, there's more.

"aaa".IndexOf(Convert.ToChar("a"))
DOES return 0. So, the behaviour is fine for the overload
that handles chars, but not strings.

Is what I'm seeing here a bug, or is it something normal
and I'm just being clueless about the use of indexOf ?

thanks in advance

btw - I am running on .Net framework 1.1.
 
Shukri Adams said:
I'm getting a weird error with IndexOf. It only occurs if
the substring length is 1.

"aaa".IndexOf("a")
for example should return 0 , ie, it should find the
first "a" in "aaa". Instead, it return 2, the last "a".

No it doesn't - not on my system, anyway.

using System;

public class Test
{
static void Main()
{
Console.WriteLine ("aaa".IndexOf("a"));
}
}

prints 0 on my box.

Can you see the problem with the above code? How are you running your
code?
 
Back
Top