且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VBS去除字符串的重复项并统计重复字符出现的次数

更新时间:2022-08-13 15:12:23

介绍一下思路:

(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项的值是否相等,如果相等就自增计数器;

"abcdabcd323f3f3ef3efwedwewdwedwdewe3r435t4wdewdwe"""ForToForToIfThen" "' teststr=Replace(teststr,Mid(teststr,i,1),"")EndIfNextIfAnd" "Then"字符为:""出现次数为:"EndifNext

MsgBox result



本文转自hcy's workbench博客园博客,原文链接:http://www.cnblogs.com/alterhu/archive/2012/04/08/2437629.html,如需转载请自行联系原作者。