Results 1 to 3 of 3

Thread: how to separate out words from a text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    2

    how to separate out words from a text file

    I have a text file like this:

    name=xyz
    user=asdfjklas
    etc...

    i need to have a string called name with "xyz" in it, and a string called user with "asdfjlas" in it.

    Can someone help me! I have spent like 4 hours on it already thanks!

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: how to separate out words from a text file

    4 hours? Really? IO.File.ReadAllLines Store the result of that in a variable. Loop through each line in the array returned. Use some If logic to determine whether the line is indicating a Name or a User field. Then call String.Split on the line and retrieve the item at index 1, use the '=' char as the split parameter.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: how to separate out words from a text file

    try this. i'm assuming you're using vb2008/10 as you haven't specified a version either in your profile or your thread:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim lines() As String = IO.File.ReadAllLines("yout textFile.txt")
    5.         Dim name As String = lines.FirstOrDefault(Function(s) s.StartsWith("name")).Split("="c)(1)
    6.         Dim user As String = lines.FirstOrDefault(Function(s) s.StartsWith("user")).Split("="c)(1)
    7.         'as requested
    8.     End Sub
    9.  
    10. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width