Solved

Search within text file

Posted on 2011-03-24
4
393 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
4 Comments
 
LVL 5

Expert Comment

by:soujanya_g
ID: 35205898
0
 
LVL 85

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 500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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