首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vblf上的字符串解析代码中断

vblf上的字符串解析代码中断
EN

Stack Overflow用户
提问于 2013-11-19 19:35:57
回答 2查看 613关注 0票数 1

我读了一个文本文件的行,但在空行上中断。

代码语言:javascript
复制
Using sr As New StreamReader(openFileDialog1.OpenFile())

    Dim text As [String] = sr.ReadToEnd()
    Dim lines As String() = text.Split(vbCrLf)
    For Each line As String In lines

        Dim cleanline = line.Trim()
        Dim words As String() = cleanline.Split(" ")
        For Each word As String In words
            If word.StartsWith("a", True, Nothing) OrElse word.StartsWith("e", True, Nothing) OrElse word.StartsWith("i", True, Nothing) OrElse word.StartsWith("o", True, Nothing) OrElse word.StartsWith("u", True, Nothing) OrElse word.StartsWith("y", True, Nothing) Then
                System.Console.Write(word & "ay ")
            Else
                Dim mutated As String = word.Substring(1, word.Length - 1)
                mutated = mutated & word.Substring(0, 1) & "yay "
                System.Console.Write(mutated)
            End If
        Next
        System.Console.Write(vbLf)

    Next
End Using

我正在使用输入。

我知道这个错误:

我应该更改什么来防止这个运行时错误并继续处理?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-19 20:08:32

您应该这样做,以确保您没有这个错误:

代码语言:javascript
复制
...
For Each word As String In words
    If (word.Equals("")) Then
        Continue For
    End If
...

这将确保您永远不会尝试转换空字符串。

票数 1
EN

Stack Overflow用户

发布于 2013-11-19 19:42:39

代之以:

代码语言:javascript
复制
System.Console.Write(vbLf)

在这方面:

代码语言:javascript
复制
Console.WriteLine()
代码语言:javascript
复制
For Each line As String In lines
   If line.IsNullOrWhiteSpace() Then Exit For
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20080488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档