You're here: Snippet Directory » Microsoft .NET (154)
Language:

Text einrücken

Language: Deutsch
Programming Language: C#
Published by: Thomas
Last Update: 12/2/2007
Views: 1

License: Public Domain

Description

Nachfolgend eine schnell getippte Methode, die sich einen Text nimmt und diesen einrückt - wie man es etwa aus Mailclients gewohnt ist. Perfekt bis ins letzte Detail ist es noch nicht, aber ich habe mit Outlook verglichen - da kann sie sich dem Wettbewerb getrost stellen, denn Outlook versagt ab einem gewissen Level ebenfalls ;-).

Code

1 public static string IndentText(string text, string prefix, int lineLength) 2 { 3 4 if (string.IsNullOrEmpty(text)) 5 return text; 6 7 StringBuilder result = new StringBuilder(string.Empty); 8 string[] paragraphs = text.Split(Environment.NewLine.ToCharArray()); 9 10 for (int y = 0; y <= paragraphs.Length - 1; y++) 11 { 12 string p = paragraphs[y]; 13 if (p.Trim().Length > 0) 14 { 15 for (int i = 0; i <= p.Length; i = i + lineLength) 16 { 17 string line; 18 if (p.Length >= i + lineLength) 19 { 20 line = p.Substring(i, lineLength); 21 int lastSpace = line.LastIndexOf(" "); 22 if (lastSpace > -1 && lastSpace < lineLength) 23 line = line.Substring(0, lastSpace); 24 i = i - lineLength + lastSpace; 25 } 26 else 27 { 28 line = p.Substring(i, p.Length - i); 29 } 30 result.Append(Environment.NewLine + prefix + line.TrimStart()); 31 } 32 if (y + 1 <= paragraphs.Length - 1) 33 result.Append(Environment.NewLine + prefix); 34 } 35 } 36 37 text = result.ToString(); 38 text = text.Replace(Environment.NewLine + prefix + Environment.NewLine + prefix + prefix, Environment.NewLine + prefix + prefix); 39 40 return text; 41 42 }

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS