By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,909 Members | 2,008 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,909 IT Pros & Developers. It's quick & easy.

.IndexOf only searching beginning of string.

P: 13
This is driving me nuts. Someone please help me with this. I have a program that's reading a list of terms to search for.

The .IndexOf works fine if the term being searched for is in the first few characters. But it doesn't work if the term is anywhere else in the file.

For example. Let's say I'm searching for Notice1. If the record looks like this it'll find it...

Notice1","Some data","Some data","Some data","

If it looks like this it won't find it...

Some data","Some data",Notice1","Some data","

Here is the code...

Expand|Select|Wrap|Line Numbers
  1.  
  2.   StreamReader gcLST = new StreamReader("GC.LST");
  3.  
  4.             ArrayList gcINI = new ArrayList();
  5.  
  6.             string lineval1 = "";
  7.  
  8.             while ((lineval1 = gcLST.ReadLine()) != null)
  9.             {
  10.  
  11.                 gcINI.Add(lineval1);
  12.  
  13.             }
  14.  
  15.  
  16.  while ((line1 = input.ReadLine()) != null)
  17.             {
  18.  
  19.                 bool bFoundID1 = false;
  20.  
  21.                 for (int i1 = 0; i1 < gcINI.Count; i1++)
  22.                 {
  23.  
  24.                     if ((line1.IndexOf(gcINI[i1].ToString())) == 0)
  25.  
  26.                         bFoundID1 = true;
  27.  
  28.  
  29.                 } 
  30.  
  31.  
  32.                 if (bFoundID1)
  33.                 {
  34.  
  35.                     gcHold.WriteLine(line1);
  36. }
What am I doing wrong?
Sep 20 '07 #1
Share this Question
Share on Google+
3 Replies


Shashi Sadasivan
Expert 100+
P: 1,435
Please have a look at what IndexOf Does

Change your code:
Expand|Select|Wrap|Line Numbers
  1. if ((line1.IndexOf(gcINI[i1].ToString())) == 0)
to this

Expand|Select|Wrap|Line Numbers
  1. if ((line1.IndexOf(gcINI[i1].ToString())) >= 0)
Cheers
Sep 20 '07 #2

P: 13
Thank you so much! I knew it was somthing simple but I kept over looking it.
Sep 20 '07 #3

Shashi Sadasivan
Expert 100+
P: 1,435
Its always those little things that create the big problems! :(

Cheers mate!
Sep 20 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.