根据C#中的字符数将长字符串拆分为子字符串

时间:2009-12-17 04:26:23

标签: c# string split

我想拆分看起来像这样的长字符串

weygjjsgdgkweygwjiewlewegygciefewjknfkeuwyfjkdygwfsn

进入

“weygjjsgdgk”

“weygwjiewle”

“wegygciefew”

“jknfkeuwyfj”

“kdygwfsn”

以10个字符的大块...并将其格式化为“”外面......我该怎么做?

7 个答案:

答案 0 :(得分:3)

正则表达式方法

string instr = "weygjjsgdgkweygwjiewlewegygciefewjknfkeuwyfjkdygwfsn";

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

Console.WriteLine(results);

结果

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

还有一种方法:

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

使用示例:

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

结果:

some 
very 
long 
str

答案 3 :(得分:0)

您可以使用Substring methodsSubstring(int startIndex, int length)Substring(int startIndex)。我几乎没有做任何C#,但我认为它看起来像这样:

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

可能稍微快一些的方法 - 对于更长的字符串应该更快。

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

答案 6 :(得分:0)

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