如何实现两个字符串的比较?

evanwang1975 2009-12-27 11:14:02
比如:
串1: abcde
串2: bbddef

求相同位置有几个同样的,上面同位置一样的是3。
...全文
71 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangzhl 2009-12-27
  • 打赏
  • 举报
回复
这就要好好学好数据结构了哦
tianliang1 2009-12-27
  • 打赏
  • 举报
回复
顶2楼。。。
wangyue4 2009-12-27
  • 打赏
  • 举报
回复
char[] a = {"a","b","c","d","e"};
char[] b = {"b","b","d","d","e","f"};
int count = 0;
for(int k = 0;k<a.length;k++)
{
if(a[k] == b[k])
{
count++;
}
}
lovelan1748 2009-12-27
  • 打赏
  • 举报
回复
string str1 = xxx;
string str2 = xxx;
int len = str1.Length;
if(len > str2.Length)
{
len = str2.Length;
}
//Process
char[] chars1 = str1.ToArray();
char[] chars2 = str2.ToArray();
for(int i=0 ; i < len; i++)
{
bool isEqual = (char1[i] == char2[i]);
}
十八道胡同 2009-12-27
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.IO;

namespace CSharpTest
{



class B
{

static void Main()
{
string str1 = "abcde", str2 = "bbddef";
int num=0;
if (str1.Length > str2.Length)
{
for (int i = 0; i < str2.Length; i++)
if (str2[i] == str1[i]) num++;
}
else
{
for (int i = 0; i < str1.Length; i++)
if (str2[i] == str1[i]) num++;
}
Console.WriteLine(num);
}


}

}
haominyi 2009-12-27
  • 打赏
  • 举报
回复
用循环,逐个比较,一个循环就可以

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧