76 lines
2.3 KiB
C#
76 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Eugen.ESystem.Windows.Forms
|
|
{
|
|
public static class FixLineLenght
|
|
{
|
|
/// <summary>
|
|
/// Formats a string with a fixed line length.
|
|
/// </summary>
|
|
static FixLineLenght()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Formats a string with a fixed line length.
|
|
/// </summary>
|
|
/// <param name="text">String to be formatted.</param>
|
|
/// <param name="lineLenght">Maximum line length</param>
|
|
/// <returns></returns>
|
|
public static string Format(string text, int lineLenght)
|
|
{
|
|
int pos = 0;
|
|
int spacePos = 0;
|
|
string character = "";
|
|
string temp = "";
|
|
string result = "";
|
|
|
|
while (text.Length > 0)
|
|
{
|
|
if (text.Length <= lineLenght)
|
|
{
|
|
result = String.Concat(result, text);
|
|
text = "";
|
|
}
|
|
else
|
|
{
|
|
while (pos <= lineLenght)
|
|
{
|
|
character = text.Remove(0, pos).Remove(1);
|
|
|
|
//Bei einem Zeilenumbruch oder Wagenrücklauf eine neu Zeile beginnen
|
|
if (character == "\n" || character == "\r")
|
|
{
|
|
spacePos = text.IndexOf("\n") + 1;
|
|
break;
|
|
}
|
|
|
|
if (character == " ")
|
|
{
|
|
spacePos = pos + 1;
|
|
}
|
|
|
|
pos++;
|
|
}
|
|
|
|
temp = text.Remove(spacePos - 1);
|
|
if (temp.EndsWith("\n") || temp.EndsWith("\r"))
|
|
{
|
|
temp = temp.Remove(temp.Length - 1);// Ein schon enthaltenes '\n' oder '\r' am Ende de temporären Strings muss entfernt werden
|
|
}
|
|
text = text.Remove(0, spacePos);
|
|
result = String.Concat(result, temp, "\r\n");
|
|
pos = 0;
|
|
spacePos = 0;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|