Solved

Search within text file

Posted on 2011-03-24
4
382 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
Comment Utility
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now