开发者

Split a long string into a substring based on a count of chars in C#

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?


Regex Approach

string instr = "weygjjsgdgkweygwjiewlewegygciefewjknfkeuwyfjkdygwfsn";

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

Console.WriteLine(results);

Results

"weygjjsgdgk"
"weygwjiewle"
"wegygciefew"
"jknfkeuwyfj"
"kdygwfsn"


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());
        }
    }
}


Just one more method:

private static string[] SplitBy(string source, int count)
{
    const string Separator = "╩";
    var byCount = source.Select((c, i) => i % count == 0 ? Separator + c : c.ToString()).ToArray();
    var inString = string.Join(string.Empty, byCount);
    return inString.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
}

Use example:

var splitted = SplitBy("some very long str", 5);

Result:

some 
very 
long 
str


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";


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) + "\""


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());
}


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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜