Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IndexOf method. 1

Status
Not open for further replies.

Antzz

Programmer
Jan 17, 2001
298
US
Hey Guys,
I am having a silly problem with the .IndexOf method of a string.

[string].IndexOf("search") is supposed to return a value if "search" is found in [string] but in my case it returns a -1 value.

Have you been through this before?

Appreciate it.
 
Here ya go:
SRCH_FQ is a constant having "FQ ". I tried replacing the constant with the actual string in the expression but same result.
Data contains the string="FQ 180.23/20.03/6.99/0/LVI"
Line (1) executes ok and returns "true". The problem is line (3)

1)if (Data.StartsWith( SRCH_FQ))
2){
3)_Pointer = Data.IndexOf(SRCH_FQ);
4)while (!_Processed)
5){
6) _EndPointer = Data.IndexOf( SRCH_SEP, _Pointer);
7) if (_EndPointer > 0)
8) _Separator++;
9) else
10 _Processed = true;
 
Code:
const string SRCH_FQ="FQ";
string Data="FQ 180.23/20.03/6.99/0/LVI";
int _Pointer = Data.IndexOf(SRCH_FQ);
After the above code , _Pointer is equal to 0.
It is impossible to return -1.
If you want to count the separators (/) or to access any value delimited by separator you could use Split() function like here:
string [] aVal =Data.Split('/');
string [] aVal = Data.Split('/');
int iCount = aVal.Length; // Number of tokens
//access each token
foreach (string str in aVal)
{
Console.WriteLine(str);
}
-obislavu-
 
I don't have my MSDN up at the moment, but could it be that IndexOf is case-sensitive?

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Hey Chiph,
Yes it is possible that IndexOf is case sensitive but in the above scenario I am looking for a "/" so it should not matter much.

obislavu, thanks for the advise. I am using your logic in my code. But it still beats me why IndexOf fails. And it sure gives me -1(I swear). Plus when I try to check out the value in the immediate window, it gives me an error that <string>.IndexOf does not exist while when I type ?<string> it does displays the contents.

If any of you guys have come across this scenario, you might wanna post a FAQ and link it up from this thread.

Thanks and appreciate your help again.
 
A correction in the above posting. I mentioned about looking for &quot;/&quot;. The first search criteria should be &quot;FQ &quot; and then &quot;/&quot;. Apologies for the mis-post.
 
The StartsWith() and IndexOf() are case sensitive and culture specific but in the example you posted I didn't see this being the cause.
If Data.StartsWith(thisString) returns true but Data.IndexOf(thisString)returns -1 then there is only one explanation: the thisString or the Data string changed its value between the above two statements.
There is maybe another thread that changes the Data string ?

-obislavu-
 
No obislavu. There is no second thread in this case. It is really bizzare. But then again it is Microsoft. So I guess I should probably expect it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top