strange behaviour with indexOf and single length strings

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.
 
J

Jon Skeet

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top