比较List中的数据

d1070193993 2016-03-13 06:13:09
在List中有一组数据
list.Add("192.168.2.0");
list.Add("192.168.3.0");
list.Add("192.168.4.0");

怎么判断里面的数据从第几位发生了改变...
...全文
362 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dalmeeme 2016-03-13
  • 打赏
  • 举报
回复
更新一下,如果没有差异,就返回-1:
			List<string> list = new List<string>();
			list.Add("192.168.2.0");
			list.Add("192.168.2.0");
			list.Add("192.168.2.0");

			string s = list[0];
			int index = s.Length;
			for (int i = 1; i < list.Count; i++)
			{
				for (int j = 0; j < s.Length; j++)
				{

					if (list[i][j] != s[j] && j < index)
						index = j;
				}

			}
			if (index == s.Length)
				index = -1;
			Console.WriteLine(index);
			Console.ReadKey();
dalmeeme 2016-03-13
  • 打赏
  • 举报
回复
			List<string> list = new List<string>();
			list.Add("192.168.2.0");
			list.Add("192.168.20.1");
			list.Add("192.168.2.0");

			string s = list[0];
			int index = s.Length - 1;
			for (int i = 1; i < list.Count; i++)
			{
				for (int j = 0; j < s.Length; j++)
				{

					if (list[i][j] != s[j] && j < index)
						index = j;
				}

			}
			Console.WriteLine(index);
			Console.ReadKey();
d1070193993 2016-03-13
  • 打赏
  • 举报
回复
引用 1 楼 LvBao_117 的回复:
没看懂你的问题,说详细些
就是 这3行数据,前面192.168.都是相同的,从后面开始不一样了,求这个变化的索引位置,用上面的数据来说,就是返回8.

111,129

社区成员

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

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

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