字符串分段

lvxiao715 2011-05-05 04:04:38
string str="11@12@13@5@9@10@88@99@23@71@17@";
五个一组
变成string[] arr={"11@12@13@5@9@","10@88@99@23@71@","17@"};
四个一组
变成string[] arr={"11@12@13@5@","9@10@88@99@","23@71@17@"};
五个、四个可以变动,有什么方法可以实现
...全文
299 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Strive超 2011-05-06
  • 打赏
  • 举报
回复
顶一下二楼的循环,很不错的。何必要用正则呀
vrhero 2011-05-05
  • 打赏
  • 举报
回复
正则未必好...正则引擎开销很大,一般只适合数据和规则都很复杂的字符串...

而你这个需求数据和规则都非常简单,不适合用正则...2楼的循环效率就不错了,用Linq则更简洁,都比正则效率高,字符串越长越明显...
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();
}
lvxiao715 2011-05-05
  • 打赏
  • 举报
回复
看来1楼8楼的正则很纯青啊
q107770540 2011-05-05
  • 打赏
  • 举报
回复
int count=4; 自己设置即可
q107770540 2011-05-05
  • 打赏
  • 举报
回复

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@
*/
}

claymore1114 2011-05-05
  • 打赏
  • 举报
回复

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();
}
dafei198607 2011-05-05
  • 打赏
  • 举报
回复
不错不错
claymore1114 2011-05-05
  • 打赏
  • 举报
回复
不行 少了@
claymore1114 2011-05-05
  • 打赏
  • 举报
回复
每隔 一定数量取 让人想起了 分页

//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();
}
lvxiao715 2011-05-05
  • 打赏
  • 举报
回复
我在拼凑字符串时,指定了一个i,当i==指定个数的时候,加入到list里,i重置为0,否则i++
不过违反了面向对象的原则,先这么用了,到时再改成楼上的方法,哈哈
我姓区不姓区 2011-05-05
  • 打赏
  • 举报
回复
另一种解法:

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();
}

我姓区不姓区 2011-05-05
  • 打赏
  • 举报
回复

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();
}

110,502

社区成员

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

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

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