您的位置:首页 > 编程语言 > VB

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

2012-04-08 14:42 525 查看
介绍一下思路:

(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

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

思路二:

两次循环遍及字符串,第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

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: