?
Solved

C# searching a text file for keywords for a word starting with # and capturing the rest of word?

Posted on 2007-10-10
7
Medium Priority
?
219 Views
Last Modified: 2008-03-06
I have the following data in a text file - I can find the file and read it line by line but how do I capture the part '0639502245' out of the following text? Is line by line the way to go or read in the whole file to buffer and then how to capture just the ser #?

SCSI device ID: port 1, ch 0, id 6, lun 0, type Medium Changer 8
Device: Plasmon Midrange-G H05d ser #:0639502245 std :3

Assume much bigger file! Thanks! -Karl66
0
Comment
Question by:Karl66
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 21

Accepted Solution

by:
surajguptha earned 1000 total points
ID: 20052112
           string a = "Device: Plasmon Midrange-G H05d ser #:0639502245 std :3";
            int start = a.IndexOf("ser #:");
            MessageBox.Show(a.Substring(start + 6, 10));

I assume here that your serial number is a 10 digit number
0
 

Author Comment

by:Karl66
ID: 20052706
Hmm thanks surajguptha! One question though using that method considering I am reading through possibly a thousand lines to find the ser # how can I search each line as I read them in to find the one on which to eprform your code above?

Thanks a ton!

Karl66
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 20052908
This is how you can read a file line by line
http://msdn2.microsoft.com/en-us/library/db5x7c0d.aspx

You can add the code to get the serial numbers within the loop
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:almightylinuxgod
ID: 20052953
Most flexable solution is to use a Regex.

The pattern for your purpose would be: "#/S*"

Regex searchRegex = new Regex("#/S*");
Match resultMatch = searchRegex.Match(inputString);
0
 

Author Comment

by:Karl66
ID: 20053477
surajguptha - thank you.

almightylinuxgod - is Regex still a mainstream C# functionality I am not overly familair with it?

Thank you both.
0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 1000 total points
ID: 20053479
Try this code:

        private void button6_Click(object sender, EventArgs e)
        {
            int indexOne = -1;  // ondex of ser #
            int indexTwo = -1;  // index of a space after a number
            string num = "";

            // Read file to list
            StreamReader sr = File.OpenText("C:\\doc4.txt");
            string input = null;
            while ((input = sr.ReadLine()) != null)
            {
                indexOne = input.IndexOf("ser #");
                if (indexOne>-1)
                {
                    indexTwo= input.IndexOf(" ", indexOne + 4);
                    num = input.Substring(indexOne + 6, indexTwo - indexOne - 6);
                    MessageBox.Show("'" + num + "'");
                    break; // exit loop
                }

            }
            sr.Close();
        }

My yest file:

============
aaa
bbb
ccc
ddd
SCSI device ID: port 1, ch 0, id 6, lun 0, type Medium Changer 8
Device: Plasmon Midrange-G H05d ser #:0639502245 std :3
fff
ggg
aaa
bbb
ccc
ddd
SCSI device ID: port 1, ch 0, id 6, lun 0, type Medium Changer 8
Device: Plasmon Midrange-G H05d ser #:0639502245 std :3
fff
ggg
=============================

If you have any questions ask.

My opinion - RegEx in this case is overkill.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 20053521
Sorry, test file is :
======
aaa
bbb
ccc
ddd
SCSI device ID: port 1, ch 0, id 6, lun 0, type Medium Changer 8
Device: Plasmon Midrange-G H05d ser #:0639502245 std :3
fff
ggg
==================
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question