Solved

Search within text file

Posted on 2011-03-24
4
392 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculate number of nights between two dates 5 53
MailAddress in vb 4 29
Code enhancement 4 20
Groupbox Control ? 2 17
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 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