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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C#'s version of VB6's instr() 1

Status
Not open for further replies.

notrut

Programmer
Feb 6, 2002
87
CA
Hi, I'm new to C# and I have a couple of questions about the Streamreader.

I would like to read info from a text file and store some, not all of the information into variables.

For example:

In a file, there is a line that has 'customername=Joe Blow'. I want to find this line and pull out the name.

How would I do this??

Before, I would read the line from the file, if it had customername in it, I would get the name.

Any help would be greatly appreciated.
 
See string.IndexOf. It is not C# specific, it is .Net Framework. The main difference is that it is 0-Basesd rather than 1-based i.e. it returns -1 for a "no find"

i = sData.IndexOf("Customer=")
if (I >= 0)

It is Case Sensitive.

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
You can use IndexOf in place of instr like this
Code:
string s = "customername=Joe Blow";
string name;
if(s.IndexOf("customername") > -1) name = s.Substring(s.IndexOf("=")+1);

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
For this kind of search you should use regular expressions which are more flexible. For your scenario:
Code:
string line=" bla  bla bla bla customername=Joe Blow";
string name="";
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("(.*)customername=(.*)");
System.Text.RegularExpressions.Match m = reg.Match(line);
if (m !=null && m.Success)
{
         // line matches with the pattern
	 name = m.Result("$2");
}
The pattern e.g. the "(.*)customername=(.*)" could be extern to the application, for instance put in the configuration file and you have no to touch the code when the pattern is changing.
Note. m.Result("$1"); will return the string before 'cusomername=' if there is.
The m.Result("$2") returns the string after 'customername=' if there is.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top