怎样将一组字符串分割成几个数组????

BH_Pirate 2010-02-10 11:59:44

string str="abcdefghijklmnopqrstuvwxyz012345678789"


每5个字符就做为一个string数组,最后剩下的字符做为一个数组.....










知识有限,不要讲太过于深奥的知识......
...全文
751 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
x102549 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lzsh0622 的回复:]

C# code
private void button1_Click(object sender, EventArgs e)
{
string str = "abcdefghijklmnopqrstuvwxyz012345678789";
int len = (int)Math.Ceiling((double)str.Length / 5);
string[] strs……
[/Quote]

对我有帮助
lzsh0622 2010-02-12
  • 打赏
  • 举报
回复
做作业而已,过客给个正式,老师还没搞清,一眼就知道不是独立完成的。
-过客- 2010-02-12
  • 打赏
  • 举报
回复
引用 12 楼 lzsh0622 的回复:
做作业而已,过客给个正式,老师还没搞清,一眼就知道不是独立完成的。
上面已经给了常规做法了,后面因为无聊,就讨论一下“茴”字怎么写了,呵呵
Dogfish 2010-02-12
  • 打赏
  • 举报
回复
引用 10 楼 zhoukang0916 的回复:
把分全给我?

吖的!搞得人家以为我倒分!!

楼主觉得你的答案好就行。
况且,倒分的没那么少。
鸭梨山大帝 2010-02-11
  • 打赏
  • 举报
回复
楼上几位包括我,貌似都很无聊呢,呵呵
鸭梨山大帝 2010-02-11
  • 打赏
  • 举报
回复
正则表达式.
[\s|\S]{1,5}
主要考虑字串中存在特殊符号+空格


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;

static void UseRegExp()
{
string str = "abcdefghijklmnopqrstuvwxyzsklo0" +
",s,21010sl,x,x!@1is2902389!*189" +
"289 s091209s s s0 20 012345678789";
List<char[]> ListCharArry = new List<char[]>();
char[] c = null;
MatchCollection mc = Regex.Matches(str,@"[\s|\S]{1,5}");
for (int i = 0; i < mc.Count; i++)
{
c = mc[i].ToString().ToCharArray();
ListCharArry.Add(c);
}
}
lzsh0622 2010-02-11
  • 打赏
  • 举报
回复
private void button1_Click(object sender, EventArgs e)
{
string str = "abcdefghijklmnopqrstuvwxyz012345678789";
int len = (int)Math.Ceiling((double)str.Length / 5);
string[] strs = new string[len];
for (int i = 0; i < len; i++)
{
strs[i] = str.Substring(i * 5, (str.Length - i * 5 > 5) ? 5 : (str.Length - i * 5));
}
}
PandaIT 2010-02-11
  • 打赏
  • 举报
回复

string str = "abcdefghijklmnopqrstuvwxyz012345678789";
int a = str.Length / 5;
if (a*5!=str.Length)
{
a++;
}
string[] arrStr=new string[a];
for (int i = 0; i < str.Length; i++)
{
arrStr[i] = str.Substring(0, 5);
str= str.Remove(0, 5);

Console.WriteLine(arrStr[i]);
if (str.Length < 5)
{
arrStr[arrStr.Length-1] = str;

}
}
for (int i = 0; i < arrStr.Length; i++)
{
Console.WriteLine(arrStr[i]);//打印结果...
}
-过客- 2010-02-11
  • 打赏
  • 举报
回复
try...

string str = "abcdefghijklmnopqrstuvwxyz012345678789";
while (str.Length > 5)
{
Console.WriteLine(str.Substring(0, 5));
str = str.Substring(5);
}
Console.WriteLine(str);
Console.ReadLine();
PandaIT 2010-02-11
  • 打赏
  • 举报
回复
把分全给我?

吖的!搞得人家以为我倒分!!
鸭梨山大帝 2010-02-11
  • 打赏
  • 举报
回复
字符分割


using System;
using System.Collections.Generic;
using System.Diagnostics;

public class Test
{
static void Main()
{
string str = "abcdefghijklmnopqrstuvwxyz012345678789";
List<char[]> ListCharArry = new List<char[]>();
char[] c = new char[5];
for (int i = 0; i < str.Length; i++)
{
if (i % 5 == 0)
{
c = str.ToCharArray(i, 5);
ListCharArry.Add(c);
continue;
}
if (i + 5 > str.Length)
{
c = str.ToCharArray(i + 1, str.Length - i - 1);
ListCharArry.Add(c);
break;
}
}
//ListCharArry <-- 结果
Console.ReadKey();
}
}
波导终结者 2010-02-11
  • 打赏
  • 举报
回复
\w{1,5}

正则
-过客- 2010-02-11
  • 打赏
  • 举报
回复
引用 7 楼 lost_painting 的回复:
楼上几位包括我,貌似都很无聊呢,呵呵


是无聊啊,明天晚上才能回家。。。

其实用正则,代码倒是可以非常简洁,不过可读性和效率都一般罢了

string str = "abcdefghijklmnopqrstuvwxyz012345678789";
string[] result = Regex.Split(str, @"(?<=^(.{5})+)(?!$)");
Dogfish 2010-02-11
  • 打赏
  • 举报
回复
我也无聊一下。

string str = "abcdefghijklmnopqrstuvwxyz012345678789";
string str2 = "";
for (int i = 0; i < str.Length; i += 5)
{
str2 += (str2 == "" ? "" : ",") + str.Substring(i, (str.Length - 5 < i ? str.Length -i: 5));
}
string[] ar = str2.Split(',');

110,535

社区成员

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

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

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