字符分割 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(); } }
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]);//打印结果... }
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)); } }
正则表达式. [\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); } }
我也无聊一下。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(',');
是无聊啊,明天晚上才能回家其实用正则,代码倒是可以非常简洁,不过可读性和效率都一般罢了string str = "abcdefghijklmnopqrstuvwxyz012345678789"; string[] result = Regex.Split(str, @"(?<=^(.{5})+)(?!$)");
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();
}
}
while (str.Length > 5)
{
Console.WriteLine(str.Substring(0, 5));
str = str.Substring(5);
}
Console.WriteLine(str);
Console.ReadLine();
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]);//打印结果...
}
{
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));
}
}
[\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);
}
}
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(',');
是无聊啊,明天晚上才能回家其实用正则,代码倒是可以非常简洁,不过可读性和效率都一般罢了string str = "abcdefghijklmnopqrstuvwxyz012345678789";
string[] result = Regex.Split(str, @"(?<=^(.{5})+)(?!$)");