public static void GetItems(List<string> list1, List<string> list2) { int beginIndex = -1; int endIndex = 0; int same = 1; foreach (string s in list1) { if (beginIndex == -1) { beginIndex = list2.IndexOf(s); continue; } endIndex = list2.IndexOf(s); if (endIndex - beginIndex == same) { same++; if (list1.IndexOf(s) == list1.Count - 1) { list2.GetRange(beginIndex, same).ForEach(o => Console.Write(o)); break; } } else { list2.GetRange(beginIndex, same).ForEach(o => Console.Write(o)); Console.WriteLine(); beginIndex = endIndex; same = 1; if (list1.IndexOf(s) == list1.Count - 1) { list2.GetRange(beginIndex, same).ForEach(o => Console.Write(o)); } } } }
Document transformations: ~/CMSTransformations Shared layouts: ~/CMSLayouts Custom page template layouts: ~/CMSTemplateLayouts Web part layouts: ~/CMSWebPartLayouts Ad-hoc template layouts: ~/CMSAdhocTemplateLayouts
string A= "2,3,4,6,7,8,9,10,12,13"; string B = "1,2,3,4,5,6,7,8,9,10,11,12,13"; string[] _A = A.Split(','); List<string> _list = new List<string>(); string str = ""; for (int i = 0; i < _A.Length; i++) { str += _A[i] + ','; if (B.IndexOf(str) < 0) { _list.Add(str); str = ""; } } foreach(string s in _list) { Console.WriteLine(s+"\r\n"); }
稍微修改了一下在,也就是str += _A[i] + ',';的位置 string A= "2,3,4,6,7,8,9,10,12,13"; string B = "1,2,3,4,5,6,7,8,9,10,11,12,13"; string[] _A = A.Split(','); List<string> _list = new List<string>(); string str = ""; for (int i = 0; i < _A.Length; i++) { if (B.IndexOf(str + _A[i] + ',') < 0) { _list.Add(str); str = ""; } str += _A[i] + ','; } foreach(string s in _list) { Console.WriteLine(s+"\r\n"); }
显示两个字符串共有的数字?string A = "2,3,4,6,7,8,9,10,12,13"; string B = "1,2,3,4,5,6,7,8,9,10,11,12,13"; string[] a = A.Split(','); string[] b = B.Split(',');var ab = from i in b where a.Contains(i) select i; Console.WriteLine (string.Join (",",ab.ToArray ()));不知道LZ是不是这个意思.
两字符串
"2,3,4,6,7,8,9,10,12,13"
"1,2,3,4,5,6,7,8,9,10,11,12,13"
得到以下字符串
2,3,4
6,7,8,9,10
12,13
*/using System;
using System.Collections.Generic;class Program
{
static string[] GetStr(string s1, string s2)
{
string[] ss = s1.Split(',');
List<string> ls = new List<string>(); for (int i = 0; i < ss.Length; i++)
{
List<string> li = new List<string>();
int x, x0;
int.TryParse(ss[i], out x);
li.Add(x.ToString());
x0 = x;
for (i++; i < ss.Length; i++)
{
int.TryParse(ss[i], out x);
if (x == x0 + 1)
{
li.Add(x.ToString());
x0 = x;
}
else { i--; break; }
}
ls.Add(string.Join(",", li.ToArray()));
}
return ls.ToArray();
} static void Main()
{
string s1 = "2,3,4,6,7,8,9,10,12,13";
string s2 = "1,2,3,4,5,6,7,8,9,10,11,12,13";
string[] array = GetStr(s1, s2); // <--- 这就你要的!
foreach (string s in array)
{
Console.WriteLine(s);
}
}
}
/* 程序输出:
2,3,4
6,7,8,9,10
12,13
*/
{
int i=0;for(i=0;i<s2.length,i++)
{
if (s1.indexof(s2[i])<0)
{
s2[i]='|';
}
}
return s2.split(new char[]{'|'}); //刨除数组中的空字符串就是你要的结果
}
GetSameItems("2,3,4,6,7,8,9,10,12,13","1,2,3,4,5,6,7,8,9,10,11,12,13")
以上代码只是示意,没有调试,我想你应该明白。
两字符串
"2,3,4,6,7,8,9,10,12,13"
"1,2,3,4,5,6,7,8,9,10,11,12,13,"
得到以下字符串
2,3,4
6,7,8,9,10
12,13
*/using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;class Program
{
static string[] GetStr(string s1, string s2)
{
string p1 = Regex.Replace(s1, "([^,]+,)", "$1|");
string p2 = string.Format(@"(?!{0})\b[^,]+,?", p1);
string s3 = Regex.Replace(s2, p2, "!").Trim('!');
return Regex.Split(s3, ",?!+");
} static void Main()
{
string s1 = "2,3,4,6,7,8,9,10,12,13";
string s2 = "1,2,3,4,5,6,7,8,9,10,11,12,13";
string[] array = GetStr(s1, s2); // <--- 这就你要的!
foreach (string s in array)
{
Console.WriteLine(s);
}
}
}
/* 程序输出:
2,3,4
6,7,8,9,10
12,13
*/
{ int beginIndex = -1;
int endIndex = 0;
int same = 1;
foreach (string s in list1)
{
if (beginIndex == -1)
{
beginIndex = list2.IndexOf(s); continue;
} endIndex = list2.IndexOf(s);
if (endIndex - beginIndex == same)
{
same++;
if (list1.IndexOf(s) == list1.Count - 1)
{
list2.GetRange(beginIndex, same).ForEach(o => Console.Write(o));
break; }
}
else
{
list2.GetRange(beginIndex, same).ForEach(o => Console.Write(o));
Console.WriteLine();
beginIndex = endIndex;
same = 1; if (list1.IndexOf(s) == list1.Count - 1)
{
list2.GetRange(beginIndex, same).ForEach(o => Console.Write(o));
}
} }
}
Shared layouts: ~/CMSLayouts
Custom page template layouts: ~/CMSTemplateLayouts
Web part layouts: ~/CMSWebPartLayouts
Ad-hoc template layouts: ~/CMSAdhocTemplateLayouts
string B = "1,2,3,4,5,6,7,8,9,10,11,12,13";
string[] _A = A.Split(','); List<string> _list = new List<string>();
string str = "";
for (int i = 0; i < _A.Length; i++)
{
str += _A[i] + ',';
if (B.IndexOf(str) < 0)
{
_list.Add(str);
str = "";
}
} foreach(string s in _list)
{
Console.WriteLine(s+"\r\n");
}
string A= "2,3,4,6,7,8,9,10,12,13";
string B = "1,2,3,4,5,6,7,8,9,10,11,12,13";
string[] _A = A.Split(','); List<string> _list = new List<string>();
string str = "";
for (int i = 0; i < _A.Length; i++)
{ if (B.IndexOf(str + _A[i] + ',') < 0)
{
_list.Add(str);
str = "";
}
str += _A[i] + ',';
} foreach(string s in _list)
{
Console.WriteLine(s+"\r\n");
}
string B = "1,2,3,4,5,6,7,8,9,10,11,12,13";
string[] a = A.Split(',');
string[] b = B.Split(',');var ab = from i in b where a.Contains(i) select i;
Console.WriteLine (string.Join (",",ab.ToArray ()));不知道LZ是不是这个意思.