Reputation: 789
Hi I am trying to make a program with VB.NET that opens a text file, capitalizes the first letter of each line, limits each line to 7 words or less, and then puts each line into a list box. For example it would take a string of text like this:
and turn it into two separate lines like this:
This is the code I have right now to read each line and capitalize the first letter of the line into the list box
Try
Dim reader As New System.IO.StreamReader(filePath)
Dim textLine As String = ""
Do While reader.Peek <> -1
textLine = reader.ReadLine
textLine = textLine.Substring(0, 1).ToUpper + textLine.Substring(1)
MAIN_FORM.previewBox.Items.Add(textLine)
Loop
reader.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Now I just need to figure out how to break each line into 7 words or less.
Any help on this would be greatly appreciated.
Upvotes: 1
Views: 220
Reputation: 216313
A bit of Linq could help a lot....
Dim count As Integer = 7
Dim pos As Integer = 0
Do While reader.Peek <> -1
textLine = reader.ReadLine
' Split the line to the individual words
Dim parts = textLine.Split(" "c)
do
' Skip the previous words and take the count required
Dim block = parts.Skip(pos).Take(count).ToArray()
' position to read the next count words
pos = pos + count
if block.Count > 0 Then
block(0) = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(block(0))
end if
MAIN_FORM.previewBox.Items.Add(string.Join(" ", block))
Loop While(pos < parts.Length)
Loop
EDIT: Not sure to have tested every edge case, but this should work for any length
Upvotes: 2