2014-02-13 57 views
0

我想读取字符串中的某个值。每行是一个新的字符串,我想读取每行上的第6个整数。VB.net与子字符串拆分功能

Public Class Form1 
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles browsebtn.Click 
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then 
     Dim filename As String = OpenFileDialog1.FileName 
     Dim streamreader As New System.IO.StreamReader(filename) 
     Dim textfile As String = streamreader.ReadToEnd 
     Dim splitChar As String = vbNewLine 
     Dim day As Integer = textfile.Substring(10, 2) 
     Dim strLine() As String = day.Split(splitChar) 
     For Each line As String In strLine 

      MsgBox(day) 

     Next 


    End If 
End Sub 
End Class 

但它只返回一个数字。如果我将day设置为一个字符串而不是一个整数,它将完美工作,除了它读取整个字符串,而不是我需要的两个整数。请帮忙。我究竟做错了什么?

编辑:

输入文件看起来是这样的:

23728 121010 00004986 00 00 2 21 22 11 447 114 2 382 292 350 

23730 121010 00064120 00 00 51 19 21 12 1064 110 2 4500 572 7734 

我希望我的输出是:

10 
10 

10来自于 “121010”

+0

向我们显示输入(您的文本文件)和所需的输出?还请注意使用块 – qwr

+0

我将其添加到我的问题中。谢谢 –

+0

那么,你如何确定“10”开始和结束的位置?这是第六个整数? –

回答

1

几点建议:

  1. 使用总是配置资源(使用或试图赶上最后用 resource.close)

  2. 绝不信任用户输入。

  3. 写代码,可以根据您的代码处理足够的不希望的情况下

更正:

Try 
     Dim text As String = Nothing 

     Using streamreader As New System.IO.StreamReader("text.txt") 
      text = streamreader.ReadToEnd() 
     End Using 
     If IsNothing(text) = False Then 
      Dim strLine() As String = text.Split(New String() {Environment.NewLine}, StringSplitOptions.None) 
      For Each line As String In strLine 

       If line.Length > 12 Then MsgBox(line.Substring(10, 2)) 
      Next 
     End If 
    Catch ex As Exception 
     'filenotfound case 
    End Try 

另一种方式:

在情况下,线路输入可以是不同的(但第二个应该在我们的情况下看起来值)

然后可以使用Regex

方法如下:

Try 
    Using streamreader As New System.IO.StreamReader(file) 
     Dim line As String 
     While streamreader.Peek > 0 
      'unreaded line from file 
      line = streamreader.ReadLine() 
      'split input by non digits 
      Dim numberstrs As String() = Regex.Split(line, "\D+") 
      'second numbers last two 
      If numberstrs.Length > 1 AndAlso numberstrs(1).Length > 2 Then 
       Console.WriteLine("{0}", numberstrs(1).Substring(numberstrs(1).Length - 2, 2)) 
      End If 

     End While 


    End Using 
Catch ex As Exception 

End Try 
+0

完美的作品!谢谢!我现在可以睡了大声笑 –

2

所有您编写的代码可以在更少的行中完成,如下所示:

For Each line As String In File.ReadAllLines(fileName) 
    MessageBox.Show(line) 
Next 

像你的例子一样,一次加载整个文件到内存中,如果它是一个大文件可能会有问题。如果该文件的大小是一个问题,这将是最好只读取一行的时间,像这样:

Dim streamReader As New StreamReader(fileName) 
Dim line As String = Nothing 
Do 
    line = streamReader.ReadLine() 
    MessageBox.Show(line) 
Loop Until line Is Nothing 

然而,问题仍然存在,你怎么分割线成其个人值。如果在您的问题中出现的值由空格分隔,则可以使用line.Split将行分隔为所有值的数组。然后让这些值之一的最后两个字符,你可以使用String.SubString,像这样:

Dim streamReader As New StreamReader(fileName) 
Dim line As String = Nothing 
Do 
    line = streamReader.ReadLine() 
    Dim parts() As String = line.Split() 
    Dim wholeNumber As String = parts(1) 
    Dim lastTwo As String = wholeNumber.SubString(wholeNumber.Length - 2) 
    MessageBox.Show(lastTwo) 
Loop Until line Is Nothing 
+0

第二个建议适用于阅读每一行。但line.split没有。我收到警告..“索引超出了阵列的范围。”我将5改为11,它显示了第一个数字,然后当它试图读取下一个时,它再次给了我警告。 –

+0

感谢您的回答,@qwr给我的答案奏效了。 –

1

史蒂芬的回答让你最那里,但不是这样一路。值得注意的是,你实际上并不想要第6个整数,因为它可能是1或2或几乎任何东西,这取决于你如何分割它。另外,在你的例子中,你说你想从121010中得到10,这可能是从该字符串的该部分的第二组两个数字或第三组两个数字。

我注意到在你的示例字符串中你有一些双空格:你需要将它排序,否则使用String.Split会给你数组中的空元素。实际上,使用parts(5)就像Steven上面使用的那样,由于双倍空间的原因,给了你一个空的元素,而这不是你想要的。你会想要parts(2),然后你需要SubString来获得你想要的数字。

另外,我认为更优雅的方法是使用RegEx来获取数字。假设您想要该字符串中的第二个10(以粗体显示):12 * * 10。如果您知道该字符串将永远是6个字符,总是会在输入行的第二场,你总是希望第三个和第四个数字那么这会对你:

Imports System.Text.RegularExpressions 
Imports System.IO 

Private Sub ReadFile 
    Dim rx As New Regex("^[^\s]+\s{1}\d{2}(\d{2}?)", RegexOptions.Compiled Or RegexOptions.CultureInvariant) 
    Dim streamReader As New StreamReader(fileName) 
    Dim line As String = String.Empty 

    Do While streamReader.Peek >= 0 
     line = streamReader.ReadLine() 
     MessageBox.Show(rx.Matches(line)(0).Groups(1).Value) 
    Loop 
End Sub 

我不是说这是唯一(或最优雅的)正则表达式,但它会工作,并且意味着你不必使用SubString,它不关心第一个字段有多长。它也假定字段之间有一个单独的空间,但也可以根据需要进行更改。只要你能制定一条规则来达到你想要的位置,你可以RegEx它。使用Expresso(免费且功能强大的实用程序)可帮助您构建合适的表达式。

+0

感谢您的回答,我试过这个,我得到一个错误“指定的参数超出了有效值的范围。” –

+0

@DylandeStPern你应该自己进行范围检查。实际上,这是正确的方式,特别是如果输入可能不同或将由用户提供。 – qwr

+0

输入将始终相同。它是一个日志文件,将日期保存为121010。 2012年10月10日。它将始终以这种格式。 –