2012-11-02 50 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();