以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- [求助]字符串对比的问题 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=186440) |
-- 作者:81538475 -- 发布时间:2023/5/3 15:10:00 -- [求助]字符串对比的问题 下面的一个function 需要修改一下。现在的代码中,我如果输入的:args(0)= 硬盘1:DDD|硬盘2:CCCCCC|显卡:123124 args(1) 是:硬盘1:CCCCCC|显卡:123124 下面的代码输出的结果是: 【修改】【硬盘1:DDD】修改为【硬盘1:CCCCCC】 【删除】【硬盘2:CCCCCC】 现在需要的是在这种情况下,只输出【删除】【硬盘1:DDD】,因为硬盘2只是序号发生了变化,内容并没有改变。可能是需要忽略需要的影响吧。。 Dim oldValues As String() = args(0).Split("|") Dim newValues As String() = args(1).Split("|") Dim msg As New StringBuilder() Dim oldDict As New Dictionary(Of String, String) Dim newDict As New Dictionary(Of String, String) For Each s As String In oldValues Dim parts As String() = s.Split(":") oldDict(parts(0)) = String.Join(":", parts, 1, parts.Length - 1) Next For Each s As String In newValues Dim parts As String() = s.Split(":") newDict(parts(0)) = String.Join(":", parts, 1, parts.Length - 1) Next \' Check for additions and modifications For Each key As String In newDict.Keys If oldDict.ContainsValue(newDict(key)) Then Continue For End If If oldDict.ContainsKey(key) AndAlso Not oldDict(key).Equals(newDict(key)) AndAlso Not newDict(key).Contains("不支持") Then msg.AppendFormat("【修改】【{0}:{1}】修改为【{0}:{2}】{3}", key, oldDict(key), newDict(key), vbCrLf) Else msg.AppendFormat("【新增】【{0}:{1}】{2}", key, newDict(key), vbCrLf) End If Next \' Check for deletions For Each key As String In oldDict.Keys If Not newDict.ContainsValue(oldDict(key)) AndAlso Not newDict.ContainsKey(key) Then msg.AppendFormat("【删除】【{0}:{1}】{2}", key, oldDict(key), vbCrLf) End If Next return msg.ToString()
[此贴子已经被作者于2023/5/3 15:10:12编辑过]
|
-- 作者:有点蓝 -- 发布时间:2023/5/3 15:46:00 -- 把字典的键值反过来即可,比如硬盘2:CCCCCC,键是CCCCCC,值是硬盘2 |