如何解析用点和等于分隔的数据然后添加到列表视图
How to parsing data from delimited with dot and equal then add to listview
我有如下一些数据
1000.2000=2A.3000.4000=1.5000=2,5B
需要解析数据并添加到列表视图。
我已经用数组编写了代码,但仍然无法正常工作,我需要解决方案。
Dim str As String = "1000.2000=2A.3000.4000=1.5000=2,5B"
Dim tabLine() As String
tabLine = str.Split("."c)
For k As Integer = 0 To tabLine.Length - 1
Dim listitem As ListViewItem
listitem = lvContent.Items.Add("1")
Dim sep As Char() = New Char(0) {}
sep(0) = "="c
For Each s As String In tabLine(k).Split(sep, 2)
listitem.SubItems.Add(s)
Next
Next
上面的代码显示了这个结果
-----------------
1000 | |
2000 | 2A |
3000 | |
4000 | 1 |
5000 | 2.5B |
我想要如下结果
-----------------
1000 | 2 | A
2000 | 2 | A
3000 | 1 |
4000 | 1 |
5000 | 2.5 | B
您的代码没有执行任何操作来拆分字符串的最后一部分。它只在“。”处分裂。然后是“=”。您还没有解释进行拆分的规则。以下代码假定您只想拆分最后一个字符,前提是它是一个字母。
Dim str As String = "1000.2000=2A.3000.4000=1.5000=2,5B"
Dim items As New List(Of ListViewItem)
Dim subNum As String = "", subLetter As String = ""
For Each val As String In str.Split("."c).Reverse
Dim subs() As String = val.Split("=".ToCharArray, 2)
Dim item As New ListViewItem(subs(0))
If subs.Length > 1 Then
subNum = subs(1)
subLetter = ""
If Char.IsLetter(subNum(subNum.Length - 1)) Then
subLetter = subNum(subNum.Length - 1)
subNum = subNum.Substring(0, subNum.Length - 1)
End If
End If
item.SubItems.AddRange({subNum, subLetter})
items.Add(item)
Next
items.Reverse()
lvContent.Items.AddRange(items.ToArray)
我有如下一些数据
1000.2000=2A.3000.4000=1.5000=2,5B
需要解析数据并添加到列表视图。 我已经用数组编写了代码,但仍然无法正常工作,我需要解决方案。
Dim str As String = "1000.2000=2A.3000.4000=1.5000=2,5B"
Dim tabLine() As String
tabLine = str.Split("."c)
For k As Integer = 0 To tabLine.Length - 1
Dim listitem As ListViewItem
listitem = lvContent.Items.Add("1")
Dim sep As Char() = New Char(0) {}
sep(0) = "="c
For Each s As String In tabLine(k).Split(sep, 2)
listitem.SubItems.Add(s)
Next
Next
上面的代码显示了这个结果
-----------------
1000 | |
2000 | 2A |
3000 | |
4000 | 1 |
5000 | 2.5B |
我想要如下结果
-----------------
1000 | 2 | A
2000 | 2 | A
3000 | 1 |
4000 | 1 |
5000 | 2.5 | B
您的代码没有执行任何操作来拆分字符串的最后一部分。它只在“。”处分裂。然后是“=”。您还没有解释进行拆分的规则。以下代码假定您只想拆分最后一个字符,前提是它是一个字母。
Dim str As String = "1000.2000=2A.3000.4000=1.5000=2,5B"
Dim items As New List(Of ListViewItem)
Dim subNum As String = "", subLetter As String = ""
For Each val As String In str.Split("."c).Reverse
Dim subs() As String = val.Split("=".ToCharArray, 2)
Dim item As New ListViewItem(subs(0))
If subs.Length > 1 Then
subNum = subs(1)
subLetter = ""
If Char.IsLetter(subNum(subNum.Length - 1)) Then
subLetter = subNum(subNum.Length - 1)
subNum = subNum.Substring(0, subNum.Length - 1)
End If
End If
item.SubItems.AddRange({subNum, subLetter})
items.Add(item)
Next
items.Reverse()
lvContent.Items.AddRange(items.ToArray)