介绍一下思路:
(PS:在这里一定要注意其中的字符串的替换函数replace函数,为了这个我可是折腾了不少时间)
总体来说还是遍历字符串;
思路一:
1遍历字符串A去除字符串A中所有的重复的字符串后的字符,变成非重复的字符串B;
2循环遍历字符串B将所有的字符串B中的单字符与字符串A中的单字符进行比较,如果找到了重复的重复的字符串就就加1,否则不操作;
3一次遍历完A后打印此时的操作的字符串的个数;
代码如下:
teststr="abcdabcd" result="" For i=1 To Len(teststr) If InStr(1,result,Mid(teststr,i,1),1)>0 Then ' teststr=Replace(teststr,Mid(teststr,i,1),"") WScript.Echo teststr Else result=result+Mid(teststr,i,1) End if Next MsgBox result mytest="" For k=1 To Len(result) resk=Mid(result,k,1) value=0 For t=1 To Len(teststr) rest=Mid(teststr,t,1) If resk=rest Then value=value+1 End if Next mytest=mytest&"字符串为"&resk&" 次数为:"&value&vbCrLf Next MsgBox mytest
思路二:
两次循环遍及字符串,第i项的值与第i+1项的值是否相等,如果相等就自增计数器;
teststr="abcdabcd323f3f3ef3efwedwewdwedwdewe3r435t4wdewdwe" result="" For i=1 To Len(teststr) resi=Mid(teststr,i,1) value=1 For k=i+1 To Len(teststr) resk=Mid(teststr,k,1) If resk=resi Then repstr=Mid(teststr,k-1,3) newstr=Mid(teststr,k-1,1)+" "&Mid(teststr,k+1,1) teststr=Replace(teststr,repstr,newstr) ' teststr=Replace(teststr,Mid(teststr,i,1),"") value=value+1 WScript.Echo teststr End If Next If value>0 And Mid(teststr,i,1)<>" " Then tt="字符为:"&Mid(teststr,i,1)&"出现次数为:"&value result=result&tt&vbcrlf End if Next MsgBox result