tch001$_$1$$$10,tch002$_$2$$$10,tch002$_$3$$$10,tch002$_$3$$$11
如查找tch002$_$3,如果有两个,就用tch002$_$3$$$11替换tch002$_$3$$$10,请各位大哥指点

解决方案 »

  1.   

    Option ExplicitPrivate Sub Command1_Click()
    Dim S, S1 As String
    Dim P, P1 As IntegerS = "tch001$_$1$$$10,tch002$_$2$$$10,tch002$_$3$$$10,tch002$_$3$$$11'"
    P = InStr(S, "tch002$_$3")
    If P > 0 Then
    P1 = InStr(P + 10, S, "tch002$_$3")
    If P1 > 0 Then
    S1 = Replace(S, "tch002$_$3$$$10", "tch002$_$3$$$11")
    End If
    End If
    MsgBox S1
    End Sub
      

  2.   


    Private Sub Command1_Click()
    Dim s As String, s1 As String
    Dim m%, n%, n1%
    s = "tch001$_$1$$$10,tch002$_$2$$$10,tch002$_$2$$$10"
    Label1.Caption = s
    m = Len(s)
    n = Len("tch002$_$2$$$10")
    n1 = InStr(s, "tch002$_$2$$$10")
    If n1 <> 0 Then
        s1 = Right(s, m - (n1 - 1 + n))
        If InStr(s, "tch002$_$2$$$10") <> 0 Then
        s = Replace(s, "tch002$_$2$$$10", "tch002$_$2$$$11")
        MsgBox "字符串替换成功"
        End If
    Else
    MsgBox "字符串不存在"
    End If
    Label2.Caption = s
    End Sub这是把符合要求的全部替换
      

  3.   

    有哪位大哥知道有vb script怎么编程?
      

  4.   

    如果将每个段落的格式解释为 key & "$$$" $ value 的格式,你就是要求相同的 key 必须具有相同的 value
    Option ExplicitSub Main()
        Debug.Print Normalize("tch001$_$1$$$10,tch002$_$2$$$10,tch002$_$3$$$10,tch002$_$3$$$11")
        Debug.Print Normalize("tch001$_$1$$$10,tch002$_$2$$$10,tch002$_$3$$$10,tch002$_$3$$$11,tch001$_$1$$$99")
    End SubFunction Normalize(ByVal s As String) As String
        Dim a() As String, b() As String, values As Collection, i As Long
        a = Split(s, ",")
        Set values = New Collection
        For i = UBound(a) To 0 Step -1
            b = Split(a(i), "$$$")
            a(i) = b(0) 'Key
            On Error Resume Next
            '建立 Key-Value 对应关系,相同的 Key 只有最后第一个的 Value 可以添加到集合中
            values.Add b(1), b(0)
            On Error GoTo 0
        Next
        
        For i = UBound(a) To 0 Step -1
            a(i) = a(i) & "$$$" & values(a(i))
        Next
        
        Normalize = Join(a, ",")
    End Function