VB.NET - To return the correct matching string

Asked By abinav shankar on 02-Dec-11 12:12 AM
Hi

I am reading a file using stream reader and when doing so i read the file line by line and i get the line as follows

BH01803502625DUNCAN JOHNNY 03092010M362340313

now when I search it with Johnny i have to get other two strings BH01803502625DUNCAN and 03092010M362340313 pl help in doing it

thanks
Abinav Shankar

Riley K replied to abinav shankar on 02-Dec-11 01:15 AM

When you are reading line by line First you have to check if the Line contains the Word JOHNNY, then get the

Left and right part of the string

Here is the tested ones, embed in your code

string str = "BH01803502625DUNCAN JOHNNY 03092010M362340313 ";
 
       int startIndex = 0;
       int endIndex = str.Length;
 
       string leftString = string.Empty;
       string rightString = string.Empty;
 
       int johnIndex=str.IndexOf("JOHNNY");
       if(str.Contains("JOHNNY"))
       {
         leftString = str.Substring(0,johnIndex);
         rightString = str.Substring(johnIndex+6);
       }
       Console.WriteLine(leftString+","+rightString);
       Console.ReadLine();

Regards
dipa ahuja replied to abinav shankar on 02-Dec-11 02:14 AM

Check this 
public string button1_Click(object sender, EventArgs e)
{
  string foundString = "Not Found";
    
  System.IO.StreamReader reader;
  //open file and read
  reader = new System.IO.StreamReader("d:\\test.doc");
 
  //write content to textbox
  string data = reader.ReadToEnd();
  if (data.Contains("JOHNNY "))
  {
    foundString = "JOHNNY";
  }
  reader.Close();
  return foundString;
}