0

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

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

2 回答 2

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));
}
于 2012-11-02T15:16:25.640 回答
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();
于 2012-11-02T15:18:59.047 回答