比较2个字符串以获得不同的结果

时间:2013-05-21 02:39:38

标签: asp.net vb.net vb6

我正在做一个方法,能够返回我之前的数据与

之后的数据之间的差异
Dim dataBefore As String = "Name:Alice,Age:30,Sex:Male"
Dim dataAfter As String = "Name:Alice,Age:20,Sex:Female"
mtdCompare2String(dataBefore,dataAfter)




Public Shared Function mtdCompare2String(ByVal sBefore As String, ByVal sAfter As String) As String

//what i try to do before, supposing my loop should start over here but i failed, 
//so i just removed the loop, i need someone to correct me =(

Dim intBefore As Integer = sBefore.IndexOf(",")
Dim intAfter As Integer = sAfter.IndexOf(",")
Dim sBefore As String = sBefore.SubString(0,intBefore)
Dim sAfter As String = sAfter.SubString(sAfter.IndexOf(":"),intAfter)



Dim sb As StringBuilder
sb.append(sBefore,sAfter)

return sb.toString     

End Function

预期结果

年龄:30→20,性别:男>女

1 个答案:

答案 0 :(得分:2)

这样的事情应该有效:

Public Shared Function mtdCompare2String(ByVal sBefore As String, ByVal sAfter As String) As String

    'what i try to do before, supposing my loop should start over here but i failed, 
    'so i just removed the loop, i need someone to correct me =(

    Dim Before() As String = sBefore.Split(",")
    Dim After() As String = sAfter.Split(",")
    Dim ReturnString As String = ""
    For I = 0 To Before.Length - 1
        Dim TempBefore As String = Before(I).Split(":")(1)
        Dim TempAfter As String = After(I).Split(":")(1)
        If TempBefore <> TempAfter Then
            ReturnString += Before(I).Split(":")(0) + ":" + TempBefore + ">" + TempAfter + ","
        End If
    Next
    Return ReturnString.Substring(0, ReturnString.Length - 1)
End Function