VBS去除字符串的重复项并统计重复字符出现的次数
2012-04-08 14:42
525 查看
介绍一下思路:
(PS:在这里一定要注意其中的字符串的替换函数replace函数,为了这个我可是折腾了不少时间)
总体来说还是遍历字符串;
思路一:
1遍历字符串A去除字符串A中所有的重复的字符串后的字符,变成非重复的字符串B;
2循环遍历字符串B将所有的字符串B中的单字符与字符串A中的单字符进行比较,如果找到了重复的重复的字符串就就加1,否则不操作;
3一次遍历完A后打印此时的操作的字符串的个数;
代码如下:
.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项的值是否相等,如果相等就自增计数器;
.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; }
(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; }
相关文章推荐
- 去除数组中重复项,并统计重复出现次数最多的元素及重复次数
- 去除一个字符串中出现的重复字符,并计算出出现最多次的字符的出现次数
- java统计字符串中的字符重复的次数和和出现次数 实例(输入字符串)
- 实现给定一个字符串剔除重复字符,并统计各个字符出现的次数
- C#练习——去掉字符串两端的空格、统计字符出现的次数及出现的索引位置、随机生成10个1-100之间的不重复的偶数
- java统计字符串中重复字符出现次数的方法
- 判断字符串中字符出现的次数+去除空格
- loadrunner统计字符串中指定字符出现的次数
- java基础—统计一个字符串中各个字符出现的次数
- 用map统计字符串中字符出现的次数
- js统计字符串中每个字符出现的次数
- 计算一个字符串最多重复的字符及出现次数
- Linux查找大体积文件,及vim查找统计重复字符出现的次数
- 判断一个字符串中出现次数最多的字符,统计这个次数
- Java小程序:字符串中,统计每个字符出现的次数
- 从文件读取包含数字和字母字符串,统计每个字符出现的次数,将次数输出到另外一个文件
- 统计字符串中字符出现的次数
- 统计一个字符串的重复字符出现的字数
- Java统计字符串中字符出现次数的方法示例
- 统计字符串各字符出现的次数