110,502
社区成员
发帖
与我相关
我的任务
分享
string[] get(string str, int count)
{
int i = 0;
return str.Split('@')
.Select(item => new { item, group = i++ / count })
.GroupBy(g => g.group)
.Select(s1 => string.Join("@", s1.Select(s2 => s2.item).ToArray())).ToArray();
}
void Main()
{
string str="11@12@13@5@9@10@88@99@23@71@17@";
int count=4;
int temp=1;
var ss=Regex.Split(str,"(?<=\\G([^@]+@){"+count+"})").ToDictionary(s=>temp++).Where(s=>s.Key%2>0);
ss.ToList().ForEach(s=>Console.WriteLine(s.Value));
/*
11@12@13@5@
9@10@88@99@
23@71@17@
*/
}
static string[] Get(string str, int count)
{
List<string> list = new List<string>();
var array = str.Split('@').Where(w=>w.Length!=0);
var len = array.Count() / count + (array.Count() % count == 0 ? 0 : 1);
for (var i = 0; i < len; i++)
{
list.Add(string.Join("@", array.Skip(i * count).Take(count))+"@");
}
return list.ToArray();
}
//string str = "11@12@13@5@9@10@88@99@23@71@17@";
//var query = Get(str, 4);
static string[] Get(string str, int count)
{
List<string> list = new List<string>();
var array = str.Split('@');
var len = array.Length / count + (array.Length % count == 0 ? 0 : 1);
for (var i = 0; i < len; i++)
{
list.Add(string.Join("@", array.Skip(i * count).Take(count)));
}
return list.ToArray();
}
static string[] Get(string str, int count)
{
List<string> list = new List<string>();
string[] temp = str.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < temp.Length; i += count)
{
sb.Clear();
for (int j = i; j < i + count && j < temp.Length; j++)
{
sb.AppendFormat("{0}@", temp[j]);
}
list.Add(sb.ToString());
}
return list.ToArray();
}
using System.Text.RegularExpressions;
static void Main(string[] args)
{
string str = "11@12@13@5@9@10@88@99@23@71@17@";
string[] result = Get(str, 4);
foreach (string s in result)
Console.WriteLine(s);
Console.WriteLine("---------------");
result = Get(str, 5);
foreach (string s in result)
Console.WriteLine(s);
}
static string[] Get(string str, int count)
{
List<string> list = new List<string>();
Match match = Regex.Match(str, string.Format(@"(\d+@){{{0}}}", count));
while (match.Success)
{
list.Add(match.Value);
str = str.Replace(match.Value, "");
match = Regex.Match(str, string.Format(@"(\d+@){{{0}}}", count));
}
if (!string.IsNullOrEmpty(str))
list.Add(str);
return list.ToArray();
}