Reputation: 789
Hi I have a program I am working on in VB.NET that opens a text file and reads a text file and limits the number of words per line to whatever the user sets and then capitalizes the first letter of each line.
This is the code I have that does that and it works; however, I would like to take it a step further and have it jump to a new line if it detects a punctuation mark like a period or exclamation mark, OR if it doesn't detect any punctuation for a while limit it to X amount of words per line like it is already doing.
Here is the code I have so far.
'Limit line to X amount of words
Dim reader As New System.IO.StreamReader(filePath)
Dim textLine As String
Dim count As Integer = MAIN_FORM.wordCountBox.Value
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 += 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
For example if I had it set to limit each line to 7 words I would like it to only have 7 words on each line, but if I had a particular line that was like this "Three word sentence." Even though that is only 3 words which is less than the limit of 7 I would like it to just stop there since that is a complete sentence.
FYI Previewbox is a regular listbox control.
Any help with this would be greatly appreciated.
Here is a picture explaining the output I am getting.
Upvotes: 0
Views: 520
Reputation: 728
Something like....
Dim reader As New System.IO.StreamReader(filePath)
Dim textLine As String
Dim count As Integer = MAIN_FORM.wordCountBox.Value
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
'Add check to see if period is there
Dim Periodfinder As Int32
For Periodfinder = pos To parts.GetUpperBound(0)
If parts(Periodfinder).ToString.Contains(".") Then Exit For
Next
'set periodfinder to difference between it and pos +1
Periodfinder = Periodfinder - pos + 1
'check if it is less than count
If Periodfinder < count Then count = Periodfinder
'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 += count
'reset count
count = MAIN_FORM.wordCountBox.Value
If block.Count > 0 Then
block(0) = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(block(0))
End If
MAIN_FORM.previewBox.Items.Add(String.Join(" ", block))
'or if you dont want the period to be carried through
MAIN_FORM.previewBox.Items.Add(String.Join(" ", block).Replace(".",""))
Loop While (pos < parts.Length)
Loop
Code updated to allow for multiple periods in the string.
In the example in your comment below this turns One two three four five six seven eight nine ten into:
One two three four five six seven
Eight nine ten
and one two three. four five six seven. eight nine ten into:
One two three.
Four five six severn.
Eight nine ten
(if you set the limit to 7 words)
I am outputting to console.writeline to test it.
Example in Listbox control
In certain width/height ratios AND if the multicolumn is set to True then it will put the two entries side by side rather than one above the other!
Upvotes: 1