Problems with IndexOf  
Author Message
Luisfe





PostPosted: .NET Base Class Library, Problems with IndexOf Top

I have problems with IndexOf in C#. It doesn't work properly.

This is the code:

string i = "    public MceaString getCHORA()";

int a = i.IndexOf("getC"); 

The value of int a after executing the code is -1.

Anybody knows why this happen Could you help me



.NET Development24  
 
 
Alexey Raga





PostPosted: .NET Base Class Library, Problems with IndexOf Top

I've just tried... The code is "4".

 
 
Sean Hederman





PostPosted: .NET Base Class Library, Problems with IndexOf Top

Yep, i is 4 for me too.

 
 
RizwanSharp





PostPosted: .NET Base Class Library, Problems with IndexOf Top

You may be doing somrething else wrong but the code you have posted works 100% perfect and gives 4 as an index of that string.

Try again :).

Best Regards,

Rizwan aka RizwanSharp



 
 
Luisfe





PostPosted: .NET Base Class Library, Problems with IndexOf Top

Sorry I'll point out the exact code:

string i = " public MceaString getCHORA()";

int a = i.IndexOf("getC");

After executing the code in debug mode "a" value is "-1".

I'm executing the code with Visual Studio 2003 Version 7.1.3088 and Framework 1.1 Version 1.1.4322.

Could this problem be a result of the Visual Studio version Or Framework version

If I use the next statement it works properly:

int a = i.IndexOf("getCH"); a--->22


 
 
Sean Hederman





PostPosted: .NET Base Class Library, Problems with IndexOf Top

No, I get 19 in VS2005, as well as the exact versions you have. Dunno where you get the 22 from.



 
 
Luisfe





PostPosted: .NET Base Class Library, Problems with IndexOf Top

I get 22 from the last sentence, when I'm looking for IndexOf("getCH").

Notice there are four blank spaces at the start of string "i".

Have you tried the first sentence Does it work correctly


 
 
Sean Hederman





PostPosted: .NET Base Class Library, Problems with IndexOf Top

If I manually count I get the following:

string i = " public MceaString getCHORA()";

int a = i.IndexOf("getC"); // a == 19

a = i.IndexOf("getCH"); // a == 19

If I manually count the characters, it is 19 too, I do not see where 22 comes into it at all. Are you sure the above is the code you're using, or does your code have more than one space



 
 
Luisfe





PostPosted: .NET Base Class Library, Problems with IndexOf Top

There are four spaces. The code is the following:

string i = " public MceaString getCHORA()";

Do you think the version could be the problem Or may be the framework

Thank you in advance.


 
 
Sean Hederman





PostPosted: .NET Base Class Library, Problems with IndexOf Top

Okay, I put in the 4 spaces, and I am now getting 22. No, I don't think the problem has anything to do with the framework or VS version. For one thing, I'm testing on the exact same versions you are and I cannot replicate the problem. For another, this is such an important part of the Framework that if it were as buggy as that I'm sure I would have heard about it by now.

We must be missing something here, I'm sure of it. Can you do me a favour and copy the code I've pasted below directly into a new test Console app, and run it

I know it looks virtually identical, but I've changed the names just in case there's a naming conflict somewhere (although that doesn't seem likely). Another thing that worries me is that perhaps one of your characters is from a slightly different character set or something.

A final possibility is that you're misunderstanding the de****. Just make sure you're only checking the value after the line concerned has been passed, so the yellow highlighting is on the line below.

string testString = " public MceaString getCHORA()";

int testStringPosition = testString.IndexOf("getC");

Console.WriteLine(testStringPosition);

testStringPosition = testString.IndexOf("getCH");

Console.WriteLine(testStringPosition);



 
 
Friendly Dog





PostPosted: .NET Base Class Library, Problems with IndexOf Top

Are you using a non-English environment

 
 
Luisfe





PostPosted: .NET Base Class Library, Problems with IndexOf Top

I've tried the code you tell me and the result is the same. My VS version is in English but I have my computer configured in Spanish.

Can produce this trouble

Thanks in advanced.


 
 
Luisfe





PostPosted: .NET Base Class Library, Problems with IndexOf Top

VS is an English version but my PC locale is Spanish.

Is there any problem with that configuration

Thanks.


 
 
Sean Hederman





PostPosted: .NET Base Class Library, Problems with IndexOf Top

I really can't I'm afraid. No matter what I do I do not get a -1. I am stumped as to what could be causing the problem.



 
 
Friendly Dog





PostPosted: .NET Base Class Library, Problems with IndexOf Top

Just came back from vacation.... I don't know if this problem is solved yet but my point is that your string may be in Unicode. You can try to convert your string to ASCII code and verify if they are actullay ASCII characters.