如何在文本文件VB中编辑空行 [英] How do I edit blank lines in text file VB

查看:80
本文介绍了如何在文本文件VB中编辑空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我正试图在文本文件中填充空白行,如你所见,但这里只编辑第69行是我的代码。



我尝试过:



hi all i'm trying to fill blank lines in a text file with a certain text as you will see,but only line 69 is being edited here is my code.

What I have tried:

Public Sub labelstrings()
        Dim lines As String() = File.ReadAllLines(del.filename)
        Dim v As Integer
        For v = 0 To 69 
            ReDim lines(v)
            If String.IsNullOrWhiteSpace(lines(v)) Then

                lines(v) = "ali"
                File.WriteAllLines(del.filename, lines)
            End If
        Next
        

    End Sub

推荐答案

您的代码存在很多问题。



首先,行 ReDim行(v)是完全没必要的。你认为这条线有什么作用?您可能需要阅读 ReDim 上的文档才能找到答案。



接下来,你写的是整个文件回到磁盘每次你找到一个空白行。这是非常不必要的并且会损害您的代码性能。如果你有一个包含100,000行的文件,其中1%是空行,你将把文件写回磁盘1000次。为什么?您只需要执行一次。



扫描所有行并进行替换,然后在循环完成后将文件写回。更好的是,如果你没有更换任何线路,为什么要把文件写回去?



还有什么?您使用的方法的名称没有任何意义。 LabelStrings?这意味着什么?



此外,您使用的是全局变量,而不是将文件名传递给方法。这使得你的代码更难调试,而且通用性也不高。



你的代码被硬编码为只能读取文件的70行。如果线路较少怎么办?您的代码抛出异常。如果还有更多行怎么办?你确定你不希望它处理动态数量的行吗?



最后,如果你的代码无法打开并阅读它,你的代码会抛出异常文件因任何原因。如果文件不存在会发生什么?你确定你不想处理这种情况吗?

There's plenty of problems with your code.

First, the line ReDim lines(v) is completely unnecessary. What did you think this line does? You might want to read the documentation on ReDim to find out.

Next, you're writing the entire file back to disk EVERY TIME YOU FIND A BLANK LINE. That's very unnecessary and hurts your codes performance. If you had a file with 100,000 lines in it an 1% of them were blank lines, you'd be writing the file back to disk 1,000 times. Why? You only have to do it once.

Scan through all of your lines and do your replacements and then you write the file back AFTER the loop is done. Even better, if you haven't replaced any lines, why write the file back at all?

What else? The name of the method your using doesn't make any sense. "LabelStrings"? What does that mean?

Also, you're using "global" variables instead of passing the filename into the method. That makes your code harder to debug and less versatile.

Your code is hard coded to read just 70 lines of the file. What if there are fewer lines? Your code throws an exception. What if there are more lines? Are you sure you don't want this to handle a dynamic number of lines?

Lastly, your code will throw an exception if it can't open and read the file for any reason. What happens if the file doesn't exist? Are you sure you don't want to handle this situation?
Public Sub labelstrings()
    Dim lines As String() = File.ReadAllLines(del.filename)
    Dim v As Integer

    For v = 0 To 69
        If String.IsNullOrWhiteSpace(lines(v)) Then
            lines(v) = "ali"
        End If
    Next

    File.WriteAllLines(del.filename, lines)
End Sub


您的代码有很多问题,其中之一就是您只编写结果文件当有一个空行时。



使用调试器查看你的代码在做什么。它允许你逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
Your code have many problems, one of them is that you write the result file only when there is a blank line.

Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于如何在文本文件VB中编辑空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆