[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Search within text file

Posted on 2011-03-24
4
Medium Priority
?
405 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 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

Independent Software Vendors: 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!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

829 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