假设我有一个字符串:
string str = "1111222233334444";
我如何把这个字符串分成一定大小的块?
例如,将它分解为4的大小将返回字符串:
"1111"
"2222"
"3333"
"4444"
假设我有一个字符串:
string str = "1111222233334444";
我如何把这个字符串分成一定大小的块?
例如,将它分解为4的大小将返回字符串:
"1111"
"2222"
"3333"
"4444"
当前回答
我把它提升到了另一个层次。扔掉是一个简单的句子,但在我的情况下,我需要整个单词。我想我应该把它发布出来,以防有人需要类似的东西。
static IEnumerable<string> Split(string orgString, int chunkSize, bool wholeWords = true)
{
if (wholeWords)
{
List<string> result = new List<string>();
StringBuilder sb = new StringBuilder();
if (orgString.Length > chunkSize)
{
string[] newSplit = orgString.Split(' ');
foreach (string str in newSplit)
{
if (sb.Length != 0)
sb.Append(" ");
if (sb.Length + str.Length > chunkSize)
{
result.Add(sb.ToString());
sb.Clear();
}
sb.Append(str);
}
result.Add(sb.ToString());
}
else
result.Add(orgString);
return result;
}
else
return new List<string>(Regex.Split(orgString, @"(?<=\G.{" + chunkSize + "})", RegexOptions.Singleline));
}
基于以下评论的结果:
string msg = "336699AABBCCDDEEFF";
foreach (string newMsg in Split(msg, 2, false))
{
Console.WriteLine($">>{newMsg}<<");
}
Console.ReadKey();
结果:
>>33<<
>>66<<
>>99<<
>>AA<<
>>BB<<
>>CC<<
>>DD<<
>>EE<<
>>FF<<
>><<
另一种说法是:
List<string> splitData = (List<string>)Split(msg, 2, false);
for (int i = 0; i < splitData.Count - 1; i++)
{
Console.WriteLine($">>{splitData[i]}<<");
}
Console.ReadKey();
新结果:
>>33<<
>>66<<
>>99<<
>>AA<<
>>BB<<
>>CC<<
>>DD<<
>>EE<<
>>FF<<
其他回答
public static List<string> SplitByMaxLength(this string str)
{
List<string> splitString = new List<string>();
for (int index = 0; index < str.Length; index += MaxLength)
{
splitString.Add(str.Substring(index, Math.Min(MaxLength, str.Length - index)));
}
return splitString;
}
如有必要分割几个不同的长度: 例如,日期和时间的指定格式为stringstrangeStr = "07092016090532";07092016090532(日期:07.09.2016时间:09:05:32)
public static IEnumerable<string> SplitBy(this string str, int[] chunkLength)
{
if (String.IsNullOrEmpty(str)) throw new ArgumentException();
int i = 0;
for (int j = 0; j < chunkLength.Length; j++)
{
if (chunkLength[j] < 1) throw new ArgumentException();
if (chunkLength[j] + i > str.Length)
{
chunkLength[j] = str.Length - i;
}
yield return str.Substring(i, chunkLength[j]);
i += chunkLength[j];
}
}
使用:
string[] dt = strangeStr.SplitBy(new int[] { 2, 2, 4, 2, 2, 2, 2 }).ToArray();
简单而简短:
// this means match a space or not a space (anything) up to 4 characters
var lines = Regex.Matches(str, @"[\s\S]{0,4}").Cast<Match>().Select(x => x.Value);
在多芬和康他汀的答案组合中……
static IEnumerable<string> WholeChunks(string str, int chunkSize) {
for (int i = 0; i < str.Length; i += chunkSize)
yield return str.Substring(i, chunkSize);
}
这将适用于所有可以被分割成大量块的字符串,否则将抛出异常。
如果你想支持任意长度的字符串,你可以使用下面的代码:
static IEnumerable<string> ChunksUpto(string str, int maxChunkSize) {
for (int i = 0; i < str.Length; i += maxChunkSize)
yield return str.Substring(i, Math.Min(maxChunkSize, str.Length-i));
}
然而,OP明确表示他不需要这个;它有点长,很难读,稍微慢一点。本着KISS和YAGNI的精神,我选择第一个选项:它可能是最有效的实现,而且非常简短、可读,而且重要的是,它会对不符合规范的输入抛出异常。
public static List<string> DevideByStringLength(string text, int chunkSize)
{
double a = (double)text.Length / chunkSize;
var numberOfChunks = Math.Ceiling(a);
Console.WriteLine($"{text.Length} | {numberOfChunks}");
List<string> chunkList = new List<string>();
for (int i = 0; i < numberOfChunks; i++)
{
string subString = string.Empty;
if (i == (numberOfChunks - 1))
{
subString = text.Substring(chunkSize * i, text.Length - chunkSize * i);
chunkList.Add(subString);
continue;
}
subString = text.Substring(chunkSize * i, chunkSize);
chunkList.Add(subString);
}
return chunkList;
}