2012-11-02 52 views
0

我有一个字符串的说,2000个字符我怎么能拆分屏幕为70个字符并为每个70行我已经尝试了前70个字符插入新行,并为后续工作正常:分割字符串,并插入断线

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

回答

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

累了,没有看到vb删除? –

+0

我转换为vb并像魅力一样工作。谢谢 – Zaki

+0

+1“我写了这个乐趣”:D –

1

这是C#,但应该很容易为t ranslate:

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