|
-
Feb 27th, 2007, 11:39 AM
#1
Thread Starter
New Member
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
-
Feb 27th, 2007, 11:48 AM
#2
Fanatic Member
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 
-
Feb 27th, 2007, 11:50 AM
#3
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()
-
Feb 27th, 2007, 11:52 AM
#4
Frenzied Member
Re: String manipulation
try:
VB Code:
Dim sr As New System.IO.StreamReader("filename")
Dim str As String = sr.ReadLine()
Dim strAry() As String = str.Split(" ")
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
-
Feb 27th, 2007, 12:22 PM
#5
Thread Starter
New Member
Re: String manipulation
I got it working, thanks for the help!
-
Feb 27th, 2007, 12:50 PM
#6
Thread Starter
New Member
Re: String manipulation
Is it possible to make a loop stop when sr.peek reads a line containing "-1" ?
-
Feb 27th, 2007, 01:27 PM
#7
Re: String manipulation
Like this:
Code:
'Code
name = sr.ReadLine
If name = "-1" Then Exit Do
'Code
-
Feb 27th, 2007, 02:42 PM
#8
Thread Starter
New Member
Re: String manipulation
 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.
-
Feb 27th, 2007, 03:19 PM
#9
-
Feb 27th, 2007, 07:46 PM
#10
Hyperactive Member
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)
-
Feb 27th, 2007, 07:51 PM
#11
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.
-
Feb 27th, 2007, 08:49 PM
#12
Thread Starter
New Member
Re: String manipulation
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|