111,093
社区成员




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是不是这个意思.
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(',');
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");
}
/*
两字符串
"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
*/
public string[] GetSameItems(string s1,string s2)
{
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;
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
*/