我正在尝试删除ListBox中所有行项目中的所有空格。我原以为我可以抛出一个规范化的空白函数,但事实并非如此。长话短说,我正在尝试编写一个计算文件中XML字段数的程序。我想显示所有字段,但要删除它前面的所有空格(因为它格式化为XML)。
'Imports System.IO
Public Class Form1
Dim theFile As StreamReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadTheFile.Click
'Load the File
theFile = New StreamReader("C:\Users\Marc Wilson\Documents\XML\sampledata.xml")
While (theFile.Peek > -1)
ListBox1.Items.Add(theFile.ReadLine)
End While
theFile.Close()
'Only get the fields
Dim numberOfLines As Integer = ListBox1.Items.Count
For i = 0 To numberOfLines - 1
Dim theLineItem As String = ListBox1.Items.Item(i).ToString
If theLineItem.Contains("<my:") And theLineItem.Contains("/>") Then
ListBox2.Items.Add(theLineItem)
End If
Next
'Count items
lblCount.Text = ListBox2.Items.Count.ToString
End Sub
End Class
答案 0 :(得分:1)
这将删除前导和尾随空格
theFile.ReadLine.trim