Results 1 to 12 of 12

Thread: String manipulation

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    11

    String manipulation

    Hey guys/girls,

    I am still working on my practice exam for tomorrow and have another question.

    If I have a text file that I am reading with streamreader and it contains various entries, some are names and some are grades. The names are in the text file in the following format: Alice MacLeod

    What I want to know is how I could read that line and output it to a listbox in the following format: MacLeod, A

    Any help would be appreciated.

    This is what I have, but I can't seem to figure out how to get the last name. I have declared some variables that are not used yet, just ignore them.

    Sub GetInfo()
    Dim sr As IO.StreamReader
    Dim name, fName, lName As String
    Dim x, y, nameLength, space As Double
    Dim numOfGrades As Double
    sr = IO.File.OpenText("Lab5studentgrades.txt")
    Do While sr.Peek <> -1
    name = sr.ReadLine
    nameLength = CDbl(name.Length)
    lName = name.Substring(name.IndexOf(" "), nameLength - 1)
    fName = name.Substring(0, 1)
    lstOutput1.Items.Add("Name: " & lName & ", " & fName)
    Loop
    End Sub

  2. #2
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: String manipulation

    You can use the split function. Just say
    Dim array as string()
    array = name.split(" ")
    and then firstname = array(0) and lastname = array(1)

    Hope this helps
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  3. #3
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: String manipulation

    As Kimmy said,

    or to keep with your original method, although I changed the Streamreader and name length should be an Integer I think:

    Code:
            Dim name, fName, lName As String
            Dim x, y, space As Double
            Dim numOfGrades As Double = 0
            Dim nameLength As Integer
            Dim break As Integer = 0
    
    
            Dim sr As New IO.StreamReader("names.txt")
            Do While sr.Peek <> -1
                name = sr.ReadLine
                nameLength = name.Length
                fName = name.Substring(0, 1)
                break = CInt(name.IndexOf(" ") + 1)
                lName = name.Substring(break, nameLength - break)
                Me.ListBox1.Items.Add("Name: " & lName & ", " & fName)
            Loop
    
            sr.Close()
            sr.Dispose()
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  4. #4
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: String manipulation

    try:

    VB Code:
    1. Dim sr As New System.IO.StreamReader("filename")
    2. Dim str As String = sr.ReadLine()
    3. Dim strAry() As String = str.Split(" ")
    4. Dim dataYouWant As String = strAry(1) & ", " & strAry(0).SubString(0,1) 'this should output "MacLeod, A"

    You'll have to figure out the loops to go thru the whole file

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    11

    Re: String manipulation

    I got it working, thanks for the help!

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    11

    Re: String manipulation

    Is it possible to make a loop stop when sr.peek reads a line containing "-1" ?

  7. #7
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: String manipulation

    Like this:

    Code:
    'Code
    name = sr.ReadLine
    If name = "-1" Then Exit Do
    'Code
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    11

    Re: String manipulation

    Quote Originally Posted by stimbo
    Like this:

    Code:
    'Code
    name = sr.ReadLine
    If name = "-1" Then Exit Do
    'Code

    hmm..that is what I thought, but for some reason I can't get it to work.

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: String manipulation

    Whats your code?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    Re: String manipulation

    1) try it without the quotes. You might be running into a conversion issue. (you can also try explicit conversion)
    2) I have better luck with checking for < 0 than = -1. (although it should work the same in this case)

  11. #11
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: String manipulation

    He's not referring to the end of stream (at least I don't think so...)

    Code:
    sr.peek reads a line containing "-1"
    I take it he means the value of a string being "-1", hence the use of quotes.

    I could be wrong though but his response seemed to indicate the answer was fine, it just wont work. It does for me though.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    11

    Re: String manipulation

    Quote Originally Posted by stimbo
    He's not referring to the end of stream (at least I don't think so...)

    Code:
    sr.peek reads a line containing "-1"
    I take it he means the value of a string being "-1", hence the use of quotes.

    I could be wrong though but his response seemed to indicate the answer was fine, it just wont work. It does for me though.
    Yeah it is a string of "-1" that I am trying to read. I am going to work on it a bit more and see what I can do.

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