如何只读取vb.net中文本文件中的数字

时间:2013-06-21 21:44:02

标签: vb.net integer

你好人们作为主题节目我想在VB.net中做一个只获取* .txt文件中的数字的脚本

实施例

文字文件: asd4lkj5fdl jklj235

结果: 45235

我在谷歌做了一项研究,没有做任何事情,我确实在这里看到了答案,但只在C中 我知道理论上它需要像这样: 读取每个char循环询问它是否为整数将其添加到新字符串中如果不继续下一个char,请执行此操作直到流的末尾

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

尝试使用正则表达式,不包含从文件中读取文本的代码

Dim rgx As New Regex("[^\d]")
Dim result as String = rgx.Replace("asd4lkj5fdl jklj235", "")

答案 1 :(得分:0)

  1. 将文件读入字符串
  2. 循环检查每个字符是否为数字。

    Dim strTextFromFile As String = IO.File.ReadAllText("C:\filename.txt")
    
    Dim strResults As String = String.Empty
    
    For Each c As Char In strTextFromFile
    
        If IsNumeric(c) Then
    
            strResults += c
    
        End If
    
    Next
    
    MsgBox(strResults)
    

答案 2 :(得分:0)

Public Sub Test()

    Dim contents As String = File.ReadAllText("C:\\temp\\text.txt")
    Dim digits As New String(contents.Where(Function(c) Char.IsDigit(c)).ToArray())
    MessageBox.Show(digits)

End Sub