假设我有一个字符串:
string str = "1111222233334444";
我如何把这个字符串分成一定大小的块?
例如,将它分解为4的大小将返回字符串:
"1111"
"2222"
"3333"
"4444"
假设我有一个字符串:
string str = "1111222233334444";
我如何把这个字符串分成一定大小的块?
例如,将它分解为4的大小将返回字符串:
"1111"
"2222"
"3333"
"4444"
当前回答
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();
List<string> SplitString(int chunk, string input)
{
List<string> list = new List<string>();
int cycles = input.Length / chunk;
if (input.Length % chunk != 0)
cycles++;
for (int i = 0; i < cycles; i++)
{
try
{
list.Add(input.Substring(i * chunk, chunk));
}
catch
{
list.Add(input.Substring(i * chunk));
}
}
return list;
}
我个人更喜欢我的解决方案:-)
它处理:
是块大小的倍数的字符串长度。 不是块大小倍数的字符串长度。 小于块大小的字符串长度。 NULL和空字符串(抛出异常)。 块大小小于1(引发异常)。
它是作为一个扩展方法实现的,它预先计算将要生成的块的数量。它检查最后一个块,因为如果文本长度不是一个倍数,它需要更短。干净、简短、容易理解……和工作!
public static string[] Split(this string value, int chunkSize)
{
if (string.IsNullOrEmpty(value)) throw new ArgumentException("The string cannot be null.");
if (chunkSize < 1) throw new ArgumentException("The chunk size should be equal or greater than one.");
int remainder;
int divResult = Math.DivRem(value.Length, chunkSize, out remainder);
int numberOfChunks = remainder > 0 ? divResult + 1 : divResult;
var result = new string[numberOfChunks];
int i = 0;
while (i < numberOfChunks - 1)
{
result[i] = value.Substring(i * chunkSize, chunkSize);
i++;
}
int lastChunkSize = remainder > 0 ? remainder : chunkSize;
result[i] = value.Substring(i * chunkSize, lastChunkSize);
return result;
}
我在João的解决方案基础上做了一些改进。 我所做的不同之处在于,在我的方法中,你实际上可以指定是否要返回带有剩余字符的数组,或者如果结束字符不匹配所需的块长度,你是否要截断它们,我认为这是相当灵活的,代码相当直接:
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace SplitFunction
{
class Program
{
static void Main(string[] args)
{
string text = "hello, how are you doing today?";
string[] chunks = SplitIntoChunks(text, 3,false);
if (chunks != null)
{
chunks.ToList().ForEach(e => Console.WriteLine(e));
}
Console.ReadKey();
}
private static string[] SplitIntoChunks(string text, int chunkSize, bool truncateRemaining)
{
string chunk = chunkSize.ToString();
string pattern = truncateRemaining ? ".{" + chunk + "}" : ".{1," + chunk + "}";
string[] chunks = null;
if (chunkSize > 0 && !String.IsNullOrEmpty(text))
chunks = (from Match m in Regex.Matches(text,pattern)select m.Value).ToArray();
return chunks;
}
}
}
for (int i = 0; i <= Convert.ToInt32(Math.Truncate(Convert.ToDecimal(_string.Length / _chunkSize))); i++)
{
var _currentChunk = _string.Substring(i * _chunkSize, Math.Min(_chunkSize, _string.Length - i * _chunkSize));
//do something with current chunk
}