Reputation: 23
Is there a way of like that: i have a string and it has perhaps 5000 characters, my goal is to split up the string with 100 charachter per a row and produce 50 rows. if you have opinion, could you tell me in c#?
Upvotes: 1
Views: 111
Reputation: 23
string Deneme = "Deneme Deneme Deneme Deneme DenemeDeneme Deneme Deneme Deneme
Deneme Deneme Deneme DenemeDeneme ";
string Sonuc = "";
while (Deneme.Length > 0)
{
Sonuc += new String(Deneme.Take(3).ToArray()) + Environment.NewLine;
Deneme= Deneme.Remove(0, Deneme.Length >= 3 ? 3 : Deneme.Length);
}
what do you think about with this LINQ Solution?
Upvotes: 0
Reputation: 117029
Here's a LINQ method to do it:
var split =
text
.ToCharArray()
.Select((c, n) => new { c, n })
.GroupBy(cn => cn.n / 100, cn => cn.c)
.Select(x => new string(x.ToArray()));
Upvotes: 0
Reputation: 45947
i use a extension method for that:
public static IEnumerable<string> SplitByLength(this string str, int maxLength)
{
for (int index = 0; index < str.Length; index += maxLength)
{
yield return str.Substring(index, Math.Min(maxLength, str.Length - index));
}
}
Upvotes: 1