1

我正在开发 VB.NET Windows 应用程序。在 VS 2010 中。

我想得到子字符串

$CostCenterId|4^10

从下面的字符串。

PaymentMode|NEFT^$IsPaid|False^$Currency|INR-印度卢比^$CostCenterId|4^10$LedgerId|2^3$

当前字符串($CostCenterId|4^10)在序列中的位置可能会改变。但它总是在两个 $ 符号之间。我已经写了下面的代码,但是对接下来要写什么感到困惑?

Public Sub GetSubstringData()

   dim sfullString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian  
    Rupee^$CostCenterId|4^10$LedgerId|2^3$"

     Dim CostIndex As Integer
     CostIndex = sDiscription.IndexOf("CostCenterId")
     sDiscription.Substring(CostIndex,

    End Sub
4

6 回答 6

3

查看字符串的拆分函数。这允许您根据指定的分隔字符将字符串拆分为子字符串。

然后你可以这样做:

Dim sfullString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian Rupee^$CostCenterId|4^10$LedgerId|2^3$"
Debug.WriteLine("$" + sfullString.Split("$"c)(3))

结果:$CostCenterId|4^10

您可能需要进行一些错误检查以确保字符串实际上包含您期望的数据。

但是查看数据,您拥有的是一个包含键值对的字符串,因此您最好有一个属性来保存CostCenterId并提取数据,如下所示:

Public Property CostCenterId As String

Public Sub Decode(ByVal code As String)
    For Each pair As String In code.Split("$"c)
        If pair.Length > 0 AndAlso pair.Contains("|") Then
            Dim key As String = pair.Split("|"c)(0)
            Dim value As String = pair.Split("|"c)(1)
            Select Case key
                Case "CostCenterId"
                    Me.CostCenterId = value
            End Select
        End If
    Next
End Sub

然后像这样调用它:

Decode("PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian Rupee^$CostCenterId|4^10$LedgerId|2^3$")
于 2013-04-09T10:22:07.617 回答
0

这应该有效:

Dim token = "$CostCenterId"
Dim costIndexStart As Integer = sfullString.IndexOf(token)
Dim costIndexEnd As Integer = sfullString.IndexOf("$", costIndexStart + token.Length)
Dim cost As String = sfullString.Substring(costIndexStart, costIndexEnd - costIndexStart + 1)

结果:"$CostCenterId|4^10$"

如果你想省略美元符号:

Substring(costIndexStart + 1, costIndexEnd - costIndexStart - 1)
于 2013-04-09T10:24:44.317 回答
0

你的弦,

Dim xString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian Rupee^$CostCenterId|4^10$LedgerId|2^3$"

子串过程,

 xString = xString.Substring(xString.IndexOf("$CostCenter"), xString.IndexOf("$", xString.IndexOf("$CostCenter") + 1) - xString.IndexOf("$CostCenter"))
于 2013-04-09T10:35:54.780 回答
0

为什么不通过 $ 将字符串 split() 成一个数组,然后查找包含 CostCenterId 的元素

于 2013-04-09T10:19:39.293 回答
0

尝试这样的事情:

Dim CostIndex As Integer
CostIndex = sDiscription.IndexOf("CostCenterId")

auxNum = sDiscription.IndexOf("$"c, CostIndex) - CostIndex
sResult = sDiscription.SubString(CostIndex, auxNum)
于 2013-04-09T10:19:51.453 回答
0

试试这个代码:

  Dim sfullString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian" _
    & "Rupee^$CostCenterId|4^10$LedgerId|2^3$"

        Dim sp() As String = {"$"}
        Dim ar() As String = sfullString.Split(sp, StringSplitOptions.RemoveEmptyEntries)
        Array.Sort(ar)
        MsgBox("$" & ar(0))
于 2013-04-09T12:00:22.073 回答