Vb读取行,左边5个字符,复制行中的其余文本,显示并继续到下一行 [英] Vb read line, 5 characters to the left, copy rest of text in the line, display and continue to next line

查看:405
本文介绍了Vb读取行,左边5个字符,复制行中的其余文本,显示并继续到下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

逐行阅读,比较一个值并提取prtion来显示



我在c上有一个文本文件,我需要打开并读取第一行,验证5个字符是否=某些其他文本,如果是,则显示行中文本的REST,然后对file.txt上的每一行重复,直到它读取文本文件中的所有行。不知道该怎么做...



我的代码



Read line by line, compare a value and extract prtion to display

I have a text file on c, i need to open and read the first line, verify if the 5 characters are = to some other text, if yes, display the REST of the text in the line, then repeat for each line on the file.txt until it reads all the lines in the text file. not sure how to do it...

My "code"

Dim fileReader As StreamReader fileReader = My.Computer.FileSystem.OpenTextFileReader("C:\locatin1/filetoshow.txt")

    Dim stringReader As String
    stringReader = fileReader.ReadLine()
    ''if readline last end if not continue to next line....
    '' read first 6 characters and do if
    ' If stringReader = ""Dis1err" Then
    'Display1.Dis1RTB1.Text = "display the items to the right after character 6 to endofline"
    'Else
    ' If stringReader = ""Dis1jam" Then
    'Display1.Dis1RTB2.Text = "display the items to the right after character 6 to endofline"
    'Else
    ' If stringReader = ""Dis1???" Then
    'Display1.Dis1RTB3.Text = "display the items to the right after character 6 to endofline"
    'Else
    ' If stringReader = ""Dis2err" Then
    'Display2.Dis2RTB1.Text = "display the items to the right after character 6 to endofline"
    'Else
    ' If stringReader = ""Dis2jam" Then
    'Display2.Dis2RTB2.Text = "display the items to the right after character 6 to endofline"
    'Else
    ' If stringReader = ""Dis2???" Then
    'Display2.Dis1RTB3.Text = "display the items to the right after character 6 to endofline"
    'Else

    'stringReader.Close()





[UpDate]

感谢您的帮助我接受了您的建议,这就是它的样子



'每种类型发送的消息



[UpDate]
thanks for the help I took your advise and this is how it looks

'Each type of Message sent

Dim type1 As String = "d1err./"
Dim type2 As String = "d1jam./"
Dim type3 As String = "d1spe./"
Dim type4 As String = "d2err./"
Dim type5 As String = "d2jam./"
Dim type6 As String = "d2spe./"







Dim fileReader As StreamReader = My.Computer.FileSystem.OpenTextFileReader("C:\\locatin1\\filetoshow.txt")
Dim stringReader As String = fileReader.ReadLine()

While stringReader <> Nothing
    Dim LineStart As String = stringReader.Substring(0, 7)
    Dim RestOfLine As String = stringReader.Substring(7)
    ' Perform your processing here...
    If LineStart = type1 Then
        Display1.Dis1RTB1.Clear()
        Display1.Dis1RTB1.Text = RestOfLine
    ElseIf LineStart = type2 Then
        Display1.Dis1RTB2.Clear()
        Display1.Dis1RTB2.Text = RestOfLine
    ElseIf LineStart = type3 Then
        Display1.Dis1RTB3.Clear()
        Display1.Dis1RTB3.Text = RestOfLine
    ElseIf LineStart = type4 Then
        Display2.Dis2RTB1.Clear()
        Display2.Dis2RTB1.Text = RestOfLine
    ElseIf LineStart = type5 Then
        Display2.Dis2RTB2.Clear()
        Display2.Dis2RTB2.Text = RestOfLine
    ElseIf LineStart = type6 Then
        Display2.Dis2RTB3.Clear()
        Display2.Dis2RTB3.Text = RestOfLine
    End If
    stringReader = fileReader.ReadLine()
End While
fileReader.Close()





最后2个问题

1.这会读取每一行并应用每行的规则吗?



我在下面打开并阅读的文本文件示例:

< line1> d1err./机器上的错误

< line2> d1jam./ jam on machine

< line3> d1spe./

< line4> d2err./

< line5> d2jam。 /堵塞机器

< line6> d2spe./机器需要维护



非常感谢



Last 2 questions
1. will this read each line and apply the rules for each line?

example of the text file i open and read below:
<line1> d1err./ error on machine
<line2> d1jam./ jam on machine
<line3>d1spe./
<line4>d2err./
<line5>d2jam./ jam on machine
<line6>d2spe./ machine needs maintenance

much appreciated

推荐答案

您的解决方案可能如下所示:

Your solution may look like this:
Dim fileReader As StreamReader = My.Computer.FileSystem.OpenTextFileReader("C:\\locatin1\\filetoshow.txt")
Dim stringReader As String = fileReader.ReadLine()
While stringReader <> Nothing
    Dim LineStart As String = stringReader.Substring(0, 6)
    Dim RestOfLine As String = stringReader.Substring(6)
    ' Perform your processing here...
    If LineStart = "Dis1err" Then
        Display1.Dis1RTB1.Text = RestOfLine
    ElseIf LineStart = "Dis1jam" Then
        Display1.Dis1RTB2.Text = RestOfLine
    End If
    stringReader = fileReader.ReadLine()
End While
fileReader.Close()



LineStart 变量保存该行的前6个字符。 RestOfLine 变量存储该行的其余部分。使用这些变量来构建你的显示逻辑。


LineStart variable holds first 6 characters of the line. RestOfLine variable stores the rest of the line. Use these variables to build your displaying logic.


我认为你正在使用VB.Net ...



当我读到我的第一个意见是:代码(大部分行都不活跃):

- 你读第一行到字符串变量'StringReader'

- 现在你将这个变量与其他一些字符串比较,例如Dis1err

- 如果Dis1err是该行的唯一内容,这将起作用 - 但我认为该行的内容比这部分更多串。在这种情况下,您应该查看,如果是Variable StringReader.CONTAINS(Dis1err),或者它是否与它一起开始StringReader.SubString(0,6)。我想,这可以解决你的问题...



最后你应该关闭FileReader(而不是StringReader)
I think you are using VB.Net ...

When I read the Code (from which most of the lines are not active) my first opinion is :
- You read the first line into the String-Variable 'StringReader'
- now you compare this Variable with some other String like for example "Dis1err"
- this would work if "Dis1err" is the only content of the line - but I suppose that the line has more content than this partial String. In this case you should look, if the Variable StringReader.CONTAINS("Dis1err") or maybe if it begins StringReader.SubString(0,6) with it. I think, that could solve your issue ...

At the End of all you should close the FileReader (and not the StringReader)

这篇关于Vb读取行,左边5个字符,复制行中的其余文本,显示并继续到下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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