拆分字符串并插入折断线

时间:2012-11-02 14:59:54

标签: vb.net string insert split

我有一个说2000字符的字符串如何将屏幕拆分为70个字符并为前70个字符尝试的每70行插入换行符,并且工作正常如下:

Dim notes As String = ""
        If (clmAck.Notes.Count > 70) Then
            notes = clmAck.Notes.Insert(70, Environment.NewLine)
        Else

2 个答案:

答案 0 :(得分:2)

我现在写这篇文章是为了好玩:

public static class StringExtension
{
    public static string InsertSpaced(this string stringToinsertInto, int spacing, string stringToInsert)
    {
        StringBuilder stringBuilder = new StringBuilder(stringToinsertInto);
        int i = 0;
        while (i + spacing < stringBuilder.Length)
        {
            stringBuilder.Insert(i + spacing, stringToInsert);
            i += spacing + stringToInsert.Length;
        }
        return stringBuilder.ToString();
    }
}

[TestCase("123456789")]
public void InsertNewLinesTest(string arg)
{
    Console.WriteLine(arg.InsertSpaced(2,Environment.NewLine));
}

答案 1 :(得分:1)

这是C#,但应该很容易翻译:

string notes = "";
var lines = new StringBuilder();

while (notes.Length > 0)
{
    int length = Math.Min(notes.Length, 70);
    lines.AppendLine(notes.Substring(0, length));

    notes = notes.Remove(0, length);
}

notes = lines.ToString();