?
Solved

Search within text file

Posted on 2011-03-24
4
Medium Priority
?
400 Views
Last Modified: 2012-05-11
I have loaded a text file's contents into a string variable

e.g.

Title:Mr
Initials:Test
Surname:Test

I need to search through the string for "Initials:" and get the word next to it.

How can I do it?

0
Comment
Question by:ict-torquilclark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35207141
There are about a billion ways to go about it...

Here's another one:
Dim initials As String = ""
        Dim initialsFound As Boolean = False

        Dim lines() As String = System.IO.File.ReadAllLines("c:\some path\folder\file.ext")
        For Each line As String In lines
            If line.ToLower.StartsWith("initials:") Then
                initials = line.Remove(0, "initials:".Length)
                initialsFound = True
                Exit For
            End If
        Next

        If initialsFound Then
            MessageBox.Show(initials, "Initials")
        End If

Open in new window

0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 2000 total points
ID: 35211189
Here is another way:

        Dim strText As String = System.IO.File.ReadAllText("C:\folder path\test.txt")
        Dim strSearch As String = "Initials:"
        Dim strFound As String = ""
        If strText.IndexOf(strSearch) > 0 Then
            strFound = strText.Substring(strText.IndexOf(strSearch) + strSearch.Length)
            If strFound.Length > 0 Then
                strFound = strFound.Substring(0, strFound.IndexOf(vbNewLine))
                MessageBox.Show(strFound, "Initials")
            End If
        End If
0
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 35213908
Hi!

As an alternate, I used Linq to decrease LOC, have a look at this as well:
        Dim contents As New List(Of String)(IO.File.ReadAllLines("C:\abc.txt"))
        Dim keyword As String = contents.Find(Function(c) c.StartsWith("Initials:")).Split(":")(1)

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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