也许是一个基本问题,但我要说的是,我有了一个长于2000年的特点,我需要把这一扼杀分为最多512个性chu。
这样做有没有一条 n路吗?
也许是一个基本问题,但我要说的是,我有了一个长于2000年的特点,我需要把这一扼杀分为最多512个性chu。
这样做有没有一条 n路吗?
与此类似:
private IList<string> SplitIntoChunks(string text, int chunkSize)
{
List<string> chunks = new List<string>();
int offset = 0;
while (offset < text.Length)
{
int size = Math.Min(chunkSize, text.Length - offset);
chunks.Add(text.Substring(offset, size));
offset += size;
}
return chunks;
}
或者说,只是为了推翻:
private IEnumerable<string> SplitIntoChunks(string text, int chunkSize)
{
int offset = 0;
while (offset < text.Length)
{
int size = Math.Min(chunkSize, text.Length - offset);
yield return text.Substring(offset, size);
offset += size;
}
}
请注意,这分为UTF-16代码单位的chu,与统法协会编码点的 split平相差甚远,而后者则可能与将肾上腺分解成chu一样。
虽然这个问题同时得到了接受的答复,但这里的答案是简短的,得到了经常表达的帮助。 Pur客可能不喜欢(难以理解),但当你需要迅速的解决办法,而且你与reg鱼齐头并进时,情况就是如此。 业绩相当好,令人惊讶:
string [] split = Regex.Split(yourString, @"(?<=G.{512})");
它做了些什么? 否定性回击和记住最后一项立场:<代码> G 。 它也将达到最后一条轨道,即使该轨道在512年之前被分割。
利用Joon s 执行和关键词。
IEnumerable<string> Chunks(string text, int chunkSize)
{
for (int offset = 0; offset < text.Length; offset += chunkSize)
{
int size = Math.Min(chunkSize, text.Length - offset);
yield return text.Substring(offset, size);
}
}
static IEnumerable<string> Split(string str, int chunkSize)
{
int len = str.Length;
return Enumerable.Range(0, len / chunkSize).Select(i => str.Substring(i * chunkSize, chunkSize));
}
来源: a. 将体格分解成一定大小()。
根据<条码>载条码>的事实,我将敢于提供更精细的约恩解决办法。 执行类型IEvidable<char>
:
private IList<string> SplitIntoChunks(string text, int chunkSize)
{
var chunks = new List<string>();
int offset = 0;
while(offset < text.Length) {
chunks.Add(new string(text.Skip(offset).Take(chunkSize).ToArray()));
offset += chunkSize;
}
return chunks;
}
Most of the answer may have the same flaw. Given an empty text they will yield nothing. We (I) expect at least to get back that empty string (same behaviour as a split on a char not in the string, which will give back one item : that given string)
因此,我们应当至少一次(根据约恩法典):
IEnumerable<string> SplitIntoChunks (string text, int chunkSize)
{
int offset = 0;
do
{
int size = Math.Min (chunkSize, text.Length - offset);
yield return text.Substring (offset, size);
offset += size;
} while (offset < text.Length);
}
或使用( Edited): 在谈论这个问题后,我找到了比“<>>>>案文更妥善的处理方式:
IEnumerable<string> SplitIntoChunks (string text, int chunkSize)
{
if (text.Length <= chunkSize)
yield return text;
else
{
var chunkCount = text.Length / chunkSize;
var remainingSize = text.Length % chunkSize;
for (var offset = 0; offset < chunkCount; ++offset)
yield return text.Substring (offset * chunkSize, chunkSize);
// yield remaining text if any
if (remainingSize != 0)
yield return text.Substring (chunkCount * chunkSize, remainingSize);
}
}
也可在休息期间使用;
通用推广方法:
using System;
using System.Collections.Generic;
using System.Linq;
public static class IEnumerableExtensions
{
public static IEnumerable<IEnumerable<T>> SplitToChunks<T> (this IEnumerable<T> coll, int chunkSize)
{
int skipCount = 0;
while (coll.Skip (skipCount).Take (chunkSize) is IEnumerable<T> part && part.Any ())
{
skipCount += chunkSize;
yield return part;
}
}
}
class Program
{
static void Main (string[] args)
{
var col = Enumerable.Range(1,1<<10);
var chunks = col.SplitToChunks(8);
foreach (var c in chunks.Take (200))
{
Console.WriteLine (string.Join (" ", c.Select (n => n.ToString ("X4"))));
}
Console.WriteLine ();
Console.WriteLine ();
"Split this text into parts that are fifteen characters in length, surrounding each part with single quotes and output each into the console on seperate lines."
.SplitToChunks (15)
.Select(p => $" {string.Concat(p)} ")
.ToList ()
.ForEach (p => Console.WriteLine (p));
Console.ReadLine ();
}
}
某些情况类似?
Calculate eachLength = StringLength / WantedCharLength
Then for (int i = 0; i < StringLength; i += eachLength)
SubString (i, eachLength);