VB. NET extracts the specified content from the TXT file

Source: Internet
Author: User

Today, when I browsed the forum, I saw another topic about reading TXT text files. Insus. NET also wants to implement it based on its own ideas and share it with you.



Text files are complex, and retrieving data is also part of some text lines. To get more accurate data, Insus. NET writes several steps. Each step uses a class. After all, the program we write is object-oriented. First, create a text file under the site:

12, 16: 45 CST-0800 20111 322 52412, 16: 45 CST-0800 20111 642 53712, 16: 45 CST-0800 20111 752 3677View Code


For example:


The data to be extracted should be highlighted. Next, write a SourceDataList class:


This class is used to process rough data, remove the leading and trailing spaces of each line of text lines, and remove the rows with several consecutive spaces. Only one is retained. The text lines are separated by spaces, as long as the text lines are segmented into two or three sections.
Further, write another class DataParse. According to the class name, you will know that this is the first time you get the data.


This class can obtain data that is basically finalized. The text to be transferred is divided into two sections (space is the separator), which are determined by two sections and three sections.
If it is a text line with two segments. The first element is ":". After the colon is removed, you must determine whether all the characters are uppercase letters. The second element starts and ends with a single quotation mark. As long as all these conditions are met, we need the file line.
For three lines of text, the first and second elements are integers, and the third element should be all uppercase letters. The text line that meets the condition is the text line to be obtained.

Next, we will write another category to combine the data obtained above, DataStructures:



The data processed by this class is close to the data rows we need. Each row of data has three fields, one of which is a string, and the second and third fields are integer.
The last class DataHelper:

Process text files, obtain data, and collect data in the List (Of DataStructures) collection.
OK. Now we can display the obtained data. On the site, create a webpage. aspx and write it to the Page_Load event:



When running the webpage, you can see the following results:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.