您好我正在尝试使用VB.NET创建一个打开文本文件的程序,将每行的第一个字母大写,将每行限制为7 单词或更少,然后放入每行进入列表框。例如,它需要一串这样的文本:
并将其分为两个单独的行:
这是我现在读取每一行的代码,并将该行的第一个字母大写到列表框中
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
现在我只需要弄清楚如何将每一行分成7个字或更少。
对此的任何帮助将不胜感激。
答案 0 :(得分:2)
Linq可以帮到很多......
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
编辑:不确定是否测试了每个边缘情况,但这应该适用于任何长度