如何比较两个字符的各自的差异

kinghome 2009-07-19 10:19:48
如:NSTR="1|12|13"
OSTR="12|1|13|14|16"

通过程序 比较 得出的结果

NSTR' = 13
OSTR'= 1 ,14 ,16

这个比较程序该如何编写?

注意
/**

NSTR 长度可以比 OSTR 长
同理
OSTR 长度可以比 NSTR 短

**/
...全文
81 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kinghome 2009-07-20
  • 打赏
  • 举报
回复
<%
dim oldstr,newstr

oldstr="1|2"
newstr="1|3|5|7"

oldstr2=oldstr
newstr2=newstr


action="add"
action2="del"

response.write "原字符串"& oldstr &"<br/>"
response.write "新字符串"& newstr &"<br/><br/>"


addstr = CompareString(oldstr,newstr,"add")
delstr = CompareString(oldstr2,newstr2,action2)

response.write "比较得出"&action&"的字符:"&addstr
response.write "<br/>长度:"& Len(addstr)


response.write "<br/><br/>比较得出"&action2&"的字符:"&delstr
response.write "<br/>长度:"& Len(delstr)


Function CompareString(str1,str2,action)
dim restring,sum
restring=""
sum=0

if action ="add" then

if Int(len(str1))=0 then
restring = str2
else
'=========================================================
str1=split(str1,"|")
str2=split(str2,"|")

for i=0 to ubound(str2)

for j=0 to ubound(str1)
if str2(i) = str1(j) then
exit for
else
sum = sum+1
end if
next

if(sum = ubound(str1)+1) then
restring = restring & str2(i) &"|"
end if
sum=0
next
'======================================================

if len(restring)>0 then
restring=left(restring,len(restring)-1)
end if

end if


else

if Int(len(str2))=0 then'新字符串为空

restring = str1 ' 返回原字符串 作为 删除字符
else

str1=split(str1,"|")
str2=split(str2,"|")

for i=0 to ubound(str1)

for j=0 to ubound(str2)
if str2(j) = str1(i) then
exit for
else
sum = sum+1
end if
next

if(sum = ubound(str2)+1) then
restring = restring & str1(i) &"|"
end if
sum=0
next
'======================================================

if len(restring)>0 then
restring=left(restring,len(restring)-1)
end if


end if


end if



'得出需要 增添/删除 的字符
CompareString = restring

End Function
%>
kinghome 2009-07-20
  • 打赏
  • 举报
回复
3Q,问题已经解决了。
Dogfish 2009-07-20
  • 打赏
  • 举报
回复
题目有些看不明。
lzp4881 2009-07-20
  • 打赏
  • 举报
回复
看不明白,怎么得出的结果
hookee 2009-07-19
  • 打赏
  • 举报
回复
找不同的元素吗?为何 NSTR' 里有13 OSTR'里有1? 13和1不是共有的吗?


NSTR="1|12|13"
OSTR="12|1|13|14|16"

Set oNDic = CreateObject("Scripting.Dictionary")
Set oODic = CreateObject("Scripting.Dictionary")
arr = Split(NSTR,"|")
For i=0 To UBound(arr)
oNDic.Add "_" & arr(i), arr(i)
Next
arr = Split(OSTR,"|")
For i=0 To UBound(arr)
If oNDic.Exists("_" & arr(i)) Then
oNDic.Remove "_" & arr(i)
Else
oODic.Add "_" & arr(i), arr(i)
End If
Next

arr = oNDic.Items()
NSTR1 = Join(arr, ",")
arr = oODic.Items()
OSTR1 = Join(arr, ",")

response.Write NSTR1
response.Write OSTR1
ACMAIN_CHM 2009-07-19
  • 打赏
  • 举报
回复
把两个字符串 NSTR, OSTR 都 split 成为数组 arrNSTR, arrOSTR,
然后 for each s in arrNSTR ,检查 s 在 OSTR中是否存在,如果存在则 两个数组的对应元素都清空。这样剩下的就是非重复项了。

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧