两个字符串比较,取出重复字符个数。 (原创)
2011-09-01 08:42
477 查看
Function TwoStrComp(strOne As String, strTwo As String) As Integer '================================ '取出两个字符串中的重复字符个数 '短字符串中无重复字符的情况下 '================================ Dim strShort As String Dim strLong As String Dim strSub As String Dim intCount As Integer Dim intCyc As Integer intCount = 0 If strOne <> "" And strTwo <> "" Then strShort = strOne strLong = strTwo If Len(strOne) > Len(strTwo) Then strShort = strTwo strLong = strOne End If For intCyc = 0 To Len(strShort) - 1 strSub = Left(Right(strShort, Len(strShort) - intCyc), 1) If InStr(strLong, strSub) > 0 Then 'InStr函数返回找到第一个字符的位置 intCount = intCount + 1 End If Next TwoStrComp = intCount End If End Function Function TwoStrCompOnly(strOne As String, strTwo As String) As Integer '================================ '取出两个字符串中的重复字符个数 '重复字符只算1个的情况 '数组法 (也可用collection类) '================================ Dim strShort As String Dim strLong As String Dim strSub As String Dim intCount As Integer Dim intCyc As Integer Dim arrChr() As String intCount = 0 If strOne <> "" And strTwo <> "" Then strShort = strOne strLong = strTwo If Len(strOne) > Len(strTwo) Then strShort = strTwo strLong = strOne End If ReDim arrChr(Len(strShort)) For intCyc = 0 To Len(strShort) - 1 strSub = Left(Right(strShort, Len(strShort) - intCyc), 1) If Onlyone(arrChr, strSub) Then If InStr(strLong, strSub) > 0 Then 'InStr函数返回找到第一个字符的位置 intCount = intCount + 1 End If End If arrChr(intCyc) = strSub Next TwoStrCompOnly = intCount End If End Function Function TwoStrCompEx(strOne As String, strTwo As String) As Integer '================================ '取出两个字符串中的重复字符个数 '任何匹配字符都累加 '================================ Dim strShort As String Dim strLong As String Dim strSub As String Dim Strtemp As String Dim intCount As Integer Dim intCyc As Integer intCount = 0 If strOne <> "" And strTwo <> "" Then strShort = strOne strLong = strTwo If Len(strOne) > Len(strTwo) Then strShort = strTwo strLong = strOne End If For intCyc = 0 To Len(strShort) - 1 strSub = Left(Right(strShort, Len(strShort) - intCyc), 1) Strtemp = Replace(strLong, strSub, "") intCount = intCount + (Len(strLong) - Len(Strtemp)) Next TwoStrCompEx = intCount End If End Function Function TwoStrCompOnlyA(strOne As String, strTwo As String) As Integer '================================ '取出两个字符串中的重复字符个数 '按LZ要求,第二个字符串有重复,但按1个累加 '数组法 (也可用collection类) '================================ Dim strSub As String Dim intCount As Integer Dim intCyc As Integer Dim arrChr() As String intCount = 0 If strOne <> "" And strTwo <> "" Then ReDim arrChr(Len(strTwo)) For intCyc = 0 To Len(strTwo) - 1 strSub = Left(Right(strTwo, Len(strTwo) - intCyc), 1) If Onlyone(arrChr, strSub) Then If InStr(strOne, strSub) > 0 Then 'InStr函数返回找到第一个字符的位置 intCount = intCount + 1 End If End If arrChr(intCyc) = strSub Next TwoStrCompOnlyA = intCount End Function
相关文章推荐
- 比较两个等长字符串是否含有完全相同字符(忽略字符顺序)
- 求两个字符串去掉重复字母后所包含字符
- 汇编语言: 试编写一段程序,要求比较两个字符串 string1 和 string2 所含字符是否相等,如相 等则显示“MATCH”, 若不相同则显示“NO MATCH”。
- 如何将两个有重复字符在内的字符串合并成一个,并且排序
- Stirng类的常见用法:输出str长度,输出第一个"o",和最后一个"o"的索引,将str中的字符"l"替换成"m",字符串str按空格“ ”分割为2个字符串,比较这两个字符串是否相等。
- 比较两个等长字符串是否含有完全相同字符(忽略字符顺序)
- 求两个字符串去掉重复字母后所包含字符
- 【原创】打印一个string 字符串中重复字符的次数
- 6-2-1 字符串-字符串变量String-创建字符串变量-初始化字符串变量-字符串连接-输入字符串(单词、行)-对象变量的赋值-比较两个字符串是否同一个-比较两个字符串内容是否相同
- 快速比较两个字符串中字符完全相同(兄弟字符串比较)
- JS-取出字符串中重复次数最多的字符并输出
- 使用strncmp()函数比较两个字符串指定数量的字符
- 比较两个字符串中的字符,并交换
- PHP中比较两个字符串找出第一个不同字符位置例子
- 写一个函数,实现两个字符串的比较。即实现strcmp函数,s1=s2时返回0,s1!=s2时返回二者第一个不同字符的ASCII值。
- PHP中strncmp()函数比较两个字符串前2个字符是否相等的方法
- Linux基础day4,shell编程,感觉比较经典的:删除字符串中重复字符
- asp.net下比较两个等长字符串是否含有完全相同字符(忽略字符顺序)
- java比较两个字符串完全不同处高亮显示(过滤了某些字符串前面不不相干字符)