tags:

views:

235

answers:

6

I want to split a long string which looks something like this

weygjjsgdgkweygwjiewlewegygciefewjknfkeuwyfjkdygwfsn

into

"weygjjsgdgk"

"weygwjiewle"

"wegygciefew"

"jknfkeuwyfj"

"kdygwfsn"

in chunks of 10 chars...and format it like "" outside...how can I do this?

A: 

You can use the Substring methods, Substring(int startIndex, int length) and Substring(int startIndex). I've hardly done any C#, but I think it would look something like this:

string originalString = "weygjjsgdgkweygwjiewlewegygciefewjknfkeuwyfjkdygwfsn";
string newString = "";
while (originalString.Length > 10) {
    string line = originalString.Substring(0, 10);
    newString = "\"" + line + "\"\n";
    originalString = originalString.Substring(10);
}
newString = "\"" + originalString + "\"\n";
Kaleb Brasee
+1  A: 
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "012345678901234567890";
            int remaining = input.Length;

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < input.Length; i+=10)
            {                
                sb.Append('"').Append(input.Substring(i, Math.Min(10, remaining))).Append('"');
                remaining -= 10;
            }
            Console.WriteLine(sb.ToString());
        }
    }
}
Eric J.
A: 
string input = "...";

IEnumerable<string> output =
    from i in Enumerable.Range(0, (input.Length + 1) / 10)
    let start = i * 10
    let length = Math.Min(10, input.Length - start)
    select "\"" + input.Substring(start, length) + "\""
Pavel Minaev
A: 

Maybe slightly faster approach - should be faster for longer strings.

static void Main(string[] args)
{
    string input = "012345678901234567890";

    // Create a StringBuilder with enough space
    StringBuilder sb = new StringBuilder(input.Length + input.Length / 10 * 3 + 3);

    sb.Append('"');
    for (int n = 0; n < input.Length; n++)
    {
        // don't add if this is the first char
        if (n > 0 && n % 10 == 0)
        {
            sb.Append("\"\n\"");
        }
        sb.Append(input[n]);
    }
    sb.Append('"');

    Console.WriteLine(sb.ToString());
}
Simon Ottenhaus
A: 
foreach(var text in "abcdefghijklmnopqrstuvwxyz".SplitString(5))
{
    Debug.WriteLine(text);
}

public static IEnumerable<string> SplitString(this string input, int outputStringLength)
{
    var count = 0;
    while (count < input.Length)
    {
     var length = Math.Min(outputStringLength, input.Length - count);
     yield return string.Format("\"{0}\"", input.Substring(count, length));
     count += outputStringLength;
    }
}
Jens Granlund
+1  A: 

Regex Approach

string instr = "weygjjsgdgkweygwjiewlewegygciefewjknfkeuwyfjkdygwfsn";

string results = Regex.Replace(instr, "(.{1,11})", "\"$1\"\n");

Console.WriteLine(results);

Results

"weygjjsgdgk"
"weygwjiewle"
"wegygciefew"
"jknfkeuwyfj"
"kdygwfsn"
S.Mark