vb.net 读取txt文件的问题
比如某一行为
111 123 234 345
空格数不定,我怎么用split,从而得到这4个数呢?
[解决办法]
用正则表达式分割空格
[解决办法]
给你一种最简便的方法(这里说最简便是因为这是VS提供生成的,右键--插入代码段 就可以直接生成)
Dim filename As String = "C:\Test.txt"
Dim fields As String()
Dim delimiter As String = "这里写你的分隔符"
Using parser As New TextFieldParser(filename)
parser.SetDelimiters(delimiter)
While Not parser.EndOfData
' Read in the fields for the current line
fields = parser.ReadFields()
' Add code here to use data in fields variable.
End While
End Using
[解决办法]
给你一段代码你试一下:
Dim ss As String = "111 222 333 444 555 666"
Dim s1() As String = ss.Split(CChar(" "))
Dim str As String = ""
For i As Integer = 0 To s1.Length - 1
If s1(i) <> "" Then
str += IIf(i = s1.Length - 1, s1(i), s1(i) & ",")
End If
Next
Dim StrTrim() As String = str.Split(CChar(","))
MsgBox(StrTrim.Length)
[解决办法]
首先用一个递归函数,把多空格替换为单空格
Function SPC(byval strTmp as String) as String
strTmp = Replace(strTmp," "," ")
If Instr(strTmp," ")>0 then
Return SPC(strTmp)
Else
Return strTmp
End if
End Function
[解决办法]
Dim st As String = "111 123 234 345 "
Dim sttb As String() = st.Split(" ")
For i As Integer = 0 To sttb.Length - 1
If Not sttb(i) = "" Then
Dim Newsttb As String = sttb(i)
End If
Next
[解决办法]
Dim test As String = "111 123 234 345"Dim result() As String = test.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)For Each s As String In result RichTextBox2.Text += s + vbCrLfNext