2011-12-15 49 views

回答

4

是的,我會使用Substring

string myString = "Some very long string you need to output."; 

for(int i = 0; i < myString.length; i += 714) { 
    Debug.WriteLine(myString.Substring(i, Math.Min(714, myString.Length - i))); 
} 

或者,對於一個花哨的one-眼線:

foreach(var s in Regex.Matches(myString, ".{,714}")) Debug.WriteLine(s.Value); 
1

您可以使用此代碼:

static IEnumerable<string> Split(string str, int chunkSize) 
{ 
    return Enumerable.Range(0, str.Length/chunkSize) 
     .Select(i => str.Substring(i * chunkSize, chunkSize)); 
} 

Splitting a string into chunks of a certain size

,使自己的WriteLine方法

0

使用此

 int chunkSize = 700; 
      int stringLength = str.Length; 
      for (int i = 0; i < stringLength; i += chunkSize) 
      { 
       if (i + chunkSize > stringLength) chunkSize = stringLength - i; 
       Debug.WriteLine(str.Substring(i, chunkSize)); 

      } 
0

這裏解決是一個擴展方法應該做的伎倆(沒有LINQ只是爲了讓事情說清楚):

public static string[] SplitLength(this string val, int length) 
{ 
    var parts = new List<string>(); 

    for(int i = 0; i < val.Length; i += length) 
    { 
     parts.Add(val.Substring(i * length, length)); 
    } 

    return parts.ToArray(); 
}