Änderung
- Funktion komplett überarbeitet und optimiert - Trennzeichen für neue Zeile ' ' und '\' - Leichtere lesbarkeit von langen Datei-Pfaden - Keine führenden Leerzeichen mehr am Zeilenanfang - Zeilenumbrüche im Text werden erkannt und bleiben erhalten - Leerzeilen werden erkannt und bleiben erhalten
This commit is contained in:
parent
28c505f306
commit
067a6a87a0
Binary file not shown.
@ -110,64 +110,96 @@ namespace Eugen.ESystem
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string Format(string text, int lineLenght)
|
public static string Format(string text, int lineLenght)
|
||||||
{
|
{
|
||||||
int pos = 0;
|
int ratedLenght = 0;
|
||||||
int spacePos = 0;
|
int spacePos = 0;
|
||||||
|
int currentPos = 0;
|
||||||
string character = "";
|
string character = "";
|
||||||
string temp = "";
|
string resultText = "";
|
||||||
string result = "";
|
|
||||||
|
|
||||||
while (text.Length > 0)
|
|
||||||
{
|
|
||||||
if (text.Length <= lineLenght)
|
if (text.Length <= lineLenght)
|
||||||
{
|
{
|
||||||
result = String.Concat(result, text);
|
resultText = text;
|
||||||
text = "";
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
while (pos <= lineLenght)
|
while (text.Length > 0)
|
||||||
{
|
{
|
||||||
character = text.Remove(0, pos).Remove(1);
|
while (ratedLenght < lineLenght)
|
||||||
|
{
|
||||||
|
if (currentPos < text.Length)
|
||||||
|
{
|
||||||
|
character = text.Substring(currentPos, 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
//Bei einem Zeilenumbruch oder Wagenrücklauf eine neu Zeile beginnen
|
|
||||||
if (character == "\n" || character == "\r")
|
if (character == "\n" || character == "\r")
|
||||||
{
|
{
|
||||||
spacePos = text.IndexOf("\n") + 1;
|
currentPos++;
|
||||||
break;
|
ratedLenght = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (character == " " || character == "\\")
|
if (character == " " || character == "\\")
|
||||||
{
|
{
|
||||||
spacePos = pos + 1;
|
spacePos = currentPos;
|
||||||
}
|
}
|
||||||
|
|
||||||
pos++;
|
currentPos++;
|
||||||
|
ratedLenght++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (spacePos - 1 > 0)
|
if (spacePos == 0)
|
||||||
{
|
{
|
||||||
temp = text.Remove(spacePos);
|
spacePos = currentPos;
|
||||||
if (temp.EndsWith(" "))
|
|
||||||
{
|
|
||||||
temp = temp.Remove(spacePos - 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (temp.EndsWith("\n") || temp.EndsWith("\r"))
|
if (text.Length <= lineLenght || text.Length == spacePos)
|
||||||
{
|
{
|
||||||
while (temp.EndsWith("\n") || temp.EndsWith("\r"))
|
resultText = resultText + text;
|
||||||
|
text = "";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
temp = temp.Remove(temp.Length - 1);// Ein schon enthaltenes '\n' oder '\r' am Ende des temporären Strings muss entfernt werden
|
resultText = resultText + text.Remove(spacePos) + Environment.NewLine;
|
||||||
}
|
|
||||||
}
|
|
||||||
text = text.Remove(0, spacePos);
|
text = text.Remove(0, spacePos);
|
||||||
result = String.Concat(result, temp, "\r\n");
|
|
||||||
pos = 0;
|
if (text.StartsWith(" "))
|
||||||
|
{
|
||||||
|
text = text.Remove(0, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ratedLenght = 0;
|
||||||
|
currentPos = 0;
|
||||||
spacePos = 0;
|
spacePos = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
resultText = CorrectLastLine(resultText, lineLenght);
|
||||||
|
|
||||||
|
return resultText;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string CorrectLastLine(string text, int lineLenght)
|
||||||
|
{
|
||||||
|
string tempMid = "";
|
||||||
|
string tempEnd = "";
|
||||||
|
|
||||||
|
tempEnd = text.Remove(0, text.LastIndexOf(Environment.NewLine) + 2);
|
||||||
|
tempMid = text.Remove(text.LastIndexOf(Environment.NewLine));
|
||||||
|
tempMid = tempMid.Remove(0, tempMid.LastIndexOf(Environment.NewLine) + 2);
|
||||||
|
|
||||||
|
if ((tempMid.Length + tempEnd.Length) <= lineLenght)
|
||||||
|
{
|
||||||
|
return text.Remove(text.LastIndexOf(Environment.NewLine), 2);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return text;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12
Test_FixedLineLength/Form1.Designer.cs
generated
12
Test_FixedLineLength/Form1.Designer.cs
generated
@ -44,7 +44,7 @@ namespace Test_FixedLineLength
|
|||||||
// button1
|
// button1
|
||||||
//
|
//
|
||||||
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
||||||
this.button1.Location = new System.Drawing.Point(12, 483);
|
this.button1.Location = new System.Drawing.Point(12, 553);
|
||||||
this.button1.Name = "button1";
|
this.button1.Name = "button1";
|
||||||
this.button1.Size = new System.Drawing.Size(776, 23);
|
this.button1.Size = new System.Drawing.Size(776, 23);
|
||||||
this.button1.TabIndex = 0;
|
this.button1.TabIndex = 0;
|
||||||
@ -56,14 +56,14 @@ namespace Test_FixedLineLength
|
|||||||
//
|
//
|
||||||
this.label1.Location = new System.Drawing.Point(13, 13);
|
this.label1.Location = new System.Drawing.Point(13, 13);
|
||||||
this.label1.Name = "label1";
|
this.label1.Name = "label1";
|
||||||
this.label1.Size = new System.Drawing.Size(775, 90);
|
this.label1.Size = new System.Drawing.Size(775, 125);
|
||||||
this.label1.TabIndex = 1;
|
this.label1.TabIndex = 1;
|
||||||
this.label1.Text = resources.GetString("label1.Text");
|
this.label1.Text = resources.GetString("label1.Text");
|
||||||
//
|
//
|
||||||
// labelResult
|
// labelResult
|
||||||
//
|
//
|
||||||
this.labelResult.AutoSize = true;
|
this.labelResult.AutoSize = true;
|
||||||
this.labelResult.Location = new System.Drawing.Point(13, 103);
|
this.labelResult.Location = new System.Drawing.Point(13, 138);
|
||||||
this.labelResult.Name = "labelResult";
|
this.labelResult.Name = "labelResult";
|
||||||
this.labelResult.Size = new System.Drawing.Size(220, 13);
|
this.labelResult.Size = new System.Drawing.Size(220, 13);
|
||||||
this.labelResult.TabIndex = 2;
|
this.labelResult.TabIndex = 2;
|
||||||
@ -73,7 +73,7 @@ namespace Test_FixedLineLength
|
|||||||
//
|
//
|
||||||
this.groupBoxProgramInfo.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
this.groupBoxProgramInfo.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
||||||
this.groupBoxProgramInfo.Controls.Add(this.labelProgramInfo);
|
this.groupBoxProgramInfo.Controls.Add(this.labelProgramInfo);
|
||||||
this.groupBoxProgramInfo.Location = new System.Drawing.Point(12, 512);
|
this.groupBoxProgramInfo.Location = new System.Drawing.Point(12, 582);
|
||||||
this.groupBoxProgramInfo.Name = "groupBoxProgramInfo";
|
this.groupBoxProgramInfo.Name = "groupBoxProgramInfo";
|
||||||
this.groupBoxProgramInfo.Size = new System.Drawing.Size(385, 66);
|
this.groupBoxProgramInfo.Size = new System.Drawing.Size(385, 66);
|
||||||
this.groupBoxProgramInfo.TabIndex = 9;
|
this.groupBoxProgramInfo.TabIndex = 9;
|
||||||
@ -94,7 +94,7 @@ namespace Test_FixedLineLength
|
|||||||
//
|
//
|
||||||
this.groupBoxDLLInfo.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
this.groupBoxDLLInfo.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
||||||
this.groupBoxDLLInfo.Controls.Add(this.labelDLLInfo);
|
this.groupBoxDLLInfo.Controls.Add(this.labelDLLInfo);
|
||||||
this.groupBoxDLLInfo.Location = new System.Drawing.Point(403, 512);
|
this.groupBoxDLLInfo.Location = new System.Drawing.Point(403, 582);
|
||||||
this.groupBoxDLLInfo.Name = "groupBoxDLLInfo";
|
this.groupBoxDLLInfo.Name = "groupBoxDLLInfo";
|
||||||
this.groupBoxDLLInfo.Size = new System.Drawing.Size(385, 66);
|
this.groupBoxDLLInfo.Size = new System.Drawing.Size(385, 66);
|
||||||
this.groupBoxDLLInfo.TabIndex = 10;
|
this.groupBoxDLLInfo.TabIndex = 10;
|
||||||
@ -115,7 +115,7 @@ namespace Test_FixedLineLength
|
|||||||
//
|
//
|
||||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||||
this.ClientSize = new System.Drawing.Size(800, 590);
|
this.ClientSize = new System.Drawing.Size(800, 660);
|
||||||
this.Controls.Add(this.groupBoxProgramInfo);
|
this.Controls.Add(this.groupBoxProgramInfo);
|
||||||
this.Controls.Add(this.groupBoxDLLInfo);
|
this.Controls.Add(this.groupBoxDLLInfo);
|
||||||
this.Controls.Add(this.labelResult);
|
this.Controls.Add(this.labelResult);
|
||||||
|
|||||||
@ -122,7 +122,7 @@ namespace Test_FixedLineLength
|
|||||||
|
|
||||||
private void button1_Click(object sender, EventArgs e)
|
private void button1_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
string text = "Das ist ein Text, der immer nach 25 Zeichen getrennt werden soll, aber nur dann wenn das Zeichen ein Leerzeichen ist. Ist das 25. Zeichen kein Leerzeichen, dann beim letzten Leerzeichen vor der 25. Stelle trennen. Dabei werden beim Trennen keine Typografischen Regeln beachtet.\r\nIst im Text ein Zeilenvorschub, dann wird dieser erkannt und auch im formatierten Text verwendet. Leerzeilen werden aber nicht berücksichtigt.\r\nEs werden auch \\ erkannt, damit lange Verzeichnisnamen wie z.B. C:\\das\\ist\\ein\\sehr\\sehr\\sehr\\sehr\\sehr\\langer\\Pfad\\den\\wir\\hier\\haben, getrennt werden können.";
|
string text = "Das ist ein Text, der immer nach 25 Zeichen getrennt werden soll, aber nur dann wenn das Zeichen ein Leerzeichen ist. Ist das 25. Zeichen kein Leerzeichen, dann beim letzten Leerzeichen vor der 25. Stelle trennen. Dabei werden beim Trennen keine Typografischen Regeln beachtet.\r\nIst im Text ein Zeilenvorschub, dann wird dieser erkannt und auch im formatierten Text verwendet.\r\n\r\nAuch Leerzeilen werden berücksichtigt.\r\nEs werden auch \\ erkannt, damit lange Verzeichnisnamen wie z.B. C:\\das\\ist\\ein\\sehr\\sehr\\sehr\\sehr\\sehr\\langer\\Pfad\\den\\wir\\hier\\haben, getrennt werden können.";
|
||||||
int lineLength = 25;
|
int lineLength = 25;
|
||||||
|
|
||||||
//--------------------------------------------------------------
|
//--------------------------------------------------------------
|
||||||
|
|||||||
@ -120,6 +120,10 @@
|
|||||||
<data name="label1.Text" xml:space="preserve">
|
<data name="label1.Text" xml:space="preserve">
|
||||||
<value>Ursprünglicher Text:
|
<value>Ursprünglicher Text:
|
||||||
|
|
||||||
Das ist ein Text, der immer nach 25 Zeichen getrennt werden soll, aber nur dann wenn das Zeichen ein Leerzeichen ist. Ist das 25. Zeichen kein Leerzeichen, dann beim letzten Leerzeichen vor der 25. Stelle trennen. Dabei werden beim Trennen keine Typografischen Regeln beachtet. Ist im Text ein Zeilenvorschub, dann wird dieser erkannt und auch im formatierten Text verwendet. Leerzeilen werden aber nicht berücksichtigt. Es werden auch \ erkannt, damit lange Verzeichnisnamen wie z.B. C:\das\ist\ein\sehr\sehr\sehr\sehr\sehr\langer\Pfad\den\wir\hier\haben, getrennt werden können.</value>
|
Das ist ein Text, der immer nach 25 Zeichen getrennt werden soll, aber nur dann wenn das Zeichen ein Leerzeichen ist. Ist das 25. Zeichen kein Leerzeichen, dann beim letzten Leerzeichen vor der 25. Stelle trennen. Dabei werden beim Trennen keine Typografischen Regeln beachtet.
|
||||||
|
Ist im Text ein Zeilenvorschub, dann wird dieser erkannt und auch im formatierten Text verwendet.
|
||||||
|
|
||||||
|
Auch Leerzeilen werden berücksichtigt.
|
||||||
|
Es werden auch \ erkannt, damit lange Verzeichnisnamen wie z.B. C:\das\ist\\ein\sehr\sehr\sehr\sehr\sehr\langer\Pfad\den\wir\hier\haben, getrennt werden können.</value>
|
||||||
</data>
|
</data>
|
||||||
</root>
|
</root>
|
||||||
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user