0

假设我有一个字符串

   Datos = "0000.0100.0200."

我想做一些类似读取字符串的事情,当我.知道有两个值编码在 4 char 字符串中,所以对于 0000。我会得到

Dim a = 00 
Dim b = 00

然后是 0100。我会得到

Dim a = 01 
Dim b = 00

然后是 0200。我会得到

Dim a = 02 
Dim b = 00

我有

Dim Items() As String = Split(Datos, ".")
For Each oneItem As String In Items
    If Not oneItem .Length < 4 Then
       Dim a = oneItem (0) & oneItem (1)
       Dim b = oneItem (2) & oneItem (3)
       MsgBox(a)
       MsgBox(b)
    End If
Next

是否有另一种更优雅的方式从 vb.net 中的 4 个字符字符串中获取 2 个值?

4

3 回答 3

1

为什么不试试 linq:

Dim expected = Datos.Split("."c).Where(Function(e) e.Length = 4) _
     .SelectMany(Function(n) New String() {n.Substring(0, 2), n.Substring(2, 2)})
于 2013-07-25T09:44:27.940 回答
1

也许像这样..

Dim Items() As String = Split(Datos, ".")
For Each oneItem As String In Items
    If oneItem.Length = 4 Then
       Dim a = oneItem.Substring(0,2)
       Dim b = oneItem.Substring(2,2)
       MsgBox(a)
       MsgBox(b)
    End If
Next
于 2013-07-25T05:44:11.573 回答
1

好吧,如果您的格式是固定长度的xxxx.xxxx.xxxx,那么我会说仅用于Substring()获取所需的部分会更有效/优雅/可维护,如下所示:

Dim Datos As String = "0000.0100.0200."

Dim a As String = Datos.Substring(0,2)
Dim b As String = Datos.Substring(2,2)

Dim c As String = Datos.Substring(5,2)
Dim d As String = Datos.Substring(7,2)

Dim e As String = Datos.Substring(10,2)
Dim f As String = Datos.Substring(12,2)
于 2013-07-25T04:11:35.217 回答