使用断字将字符串拆分为固定长度的文本行
Splitting a string into fixed-length lines of text with word-breaking
我需要输入一个字符串并按照以下规则将其分成几行:
- 行必须小于或等于 80 个字符。
- 行必须在 space 处拆分,不能将单词一分为二。
我已经试了 3 次了,但我的代码实在是太糟糕了。
foreach (ChoiceClass c in q.ChoiceTextArray)
{
int runningIndex = 0;
int m = Math.Max(c.Description.Length - 1, 80);
int numLines = (c.Description.Length / 80) + 1;
while (numLines > 0)
{
int len = 80;
if (numLines == 1)
{
len = c.Description.Length - runningIndex;
}
string tmp = string.Empty;
if (numLines > 1)
{
tmp = c.Description.Substring(runningIndex, len);
}
else
{
if (runningIndex > c.Description.Length)
{
richTextBox2.Text += "\t" + tmp + eol;
runningIndex += tmp.Length + 1;
numLines--;
}
else
{
tmp = c.Description.Substring(runningIndex);
richTextBox2.Text += "\t" + tmp + eol;
runningIndex += tmp.Length + 1;
numLines--;
}
}
}
}
不确定代码是否比你的好得多...没有其他声明
var text =
@"this a asdf asdf asdf asdf asdf wdbwbwrthwrthw rthwrth wth wrt h wrn wrnbfb wbwbwbb s jkvjv j j o o , , mfnfnsxuiua sdf asdfas dfasd f
asdf asd fasdf asdf asdf asdf asd fasdf asd fasdf asdf asdf asdf asdf asdf asd fasdf as
df asd fasdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf ";
var sb = new StringBuilder();
var currentLine = String.Empty;
var split = text.Split(' ');
foreach (var s in split)
{
if (s.Length >= 80)
{
if (!String.IsNullOrEmpty(currentLine))
{
sb.AppendLine(currentLine);
currentLine = "";
}
sb.AppendLine(s);
continue;
}
if ((String.Format("{0} {1}", currentLine, s).Length > 80))
{
sb.AppendLine(currentLine);
currentLine = "";
}
currentLine += s + " ";
}
if (!String.IsNullOrEmpty(currentLine))
{
sb.AppendLine(currentLine);
}
var final = sb.ToString();
我现在有一个工作示例了!
private void button1_Click(object sender, EventArgs e)
{
string somethingLongToPrint = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
int i = 0;
string tmp = string.Empty;
string overrun = string.Empty;
int len = somethingLongToPrint.Length - 1; // 0-based counting
while( i < len)
{
if (overrun != string.Empty)
{
// to keep the length down to 80 and still account for overrun, i should subtract the length of overrun
// If the number of characters left in "somethingLongToPrint" is less than 80, then then
int numberOfCharactersRemaining = somethingLongToPrint.Length - i;
if (numberOfCharactersRemaining > 80)
{
tmp = overrun + somethingLongToPrint.Substring(i - overrun.Length, 80);
}
else
{
tmp = overrun + somethingLongToPrint.Substring(somethingLongToPrint.Length - numberOfCharactersRemaining);
}
}
else
{
tmp = somethingLongToPrint.Substring(i, ((80 - overrun.Length)));
}
overrun = print80CharacterLine(tmp);
i += 80;
}
}
// returns what was not printed
private string print80CharacterLine(string src)
{
string eol = "\r\n";
string whatIsLeft = string.Empty;
int index = src.LastIndexOf(" ");
string calculatedString = src.Substring(0, index);
whatIsLeft = src.Substring((index+1));
// print statement here
// do the e.Graphics.DrawString(lines[linesPrinted], someFont, brush, x, y);
textBox1.Text += calculatedString + eol;
return whatIsLeft;
}
我需要输入一个字符串并按照以下规则将其分成几行:
- 行必须小于或等于 80 个字符。
- 行必须在 space 处拆分,不能将单词一分为二。
我已经试了 3 次了,但我的代码实在是太糟糕了。
foreach (ChoiceClass c in q.ChoiceTextArray)
{
int runningIndex = 0;
int m = Math.Max(c.Description.Length - 1, 80);
int numLines = (c.Description.Length / 80) + 1;
while (numLines > 0)
{
int len = 80;
if (numLines == 1)
{
len = c.Description.Length - runningIndex;
}
string tmp = string.Empty;
if (numLines > 1)
{
tmp = c.Description.Substring(runningIndex, len);
}
else
{
if (runningIndex > c.Description.Length)
{
richTextBox2.Text += "\t" + tmp + eol;
runningIndex += tmp.Length + 1;
numLines--;
}
else
{
tmp = c.Description.Substring(runningIndex);
richTextBox2.Text += "\t" + tmp + eol;
runningIndex += tmp.Length + 1;
numLines--;
}
}
}
}
不确定代码是否比你的好得多...没有其他声明
var text =
@"this a asdf asdf asdf asdf asdf wdbwbwrthwrthw rthwrth wth wrt h wrn wrnbfb wbwbwbb s jkvjv j j o o , , mfnfnsxuiua sdf asdfas dfasd f
asdf asd fasdf asdf asdf asdf asd fasdf asd fasdf asdf asdf asdf asdf asdf asd fasdf as
df asd fasdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf ";
var sb = new StringBuilder();
var currentLine = String.Empty;
var split = text.Split(' ');
foreach (var s in split)
{
if (s.Length >= 80)
{
if (!String.IsNullOrEmpty(currentLine))
{
sb.AppendLine(currentLine);
currentLine = "";
}
sb.AppendLine(s);
continue;
}
if ((String.Format("{0} {1}", currentLine, s).Length > 80))
{
sb.AppendLine(currentLine);
currentLine = "";
}
currentLine += s + " ";
}
if (!String.IsNullOrEmpty(currentLine))
{
sb.AppendLine(currentLine);
}
var final = sb.ToString();
我现在有一个工作示例了!
private void button1_Click(object sender, EventArgs e)
{
string somethingLongToPrint = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
int i = 0;
string tmp = string.Empty;
string overrun = string.Empty;
int len = somethingLongToPrint.Length - 1; // 0-based counting
while( i < len)
{
if (overrun != string.Empty)
{
// to keep the length down to 80 and still account for overrun, i should subtract the length of overrun
// If the number of characters left in "somethingLongToPrint" is less than 80, then then
int numberOfCharactersRemaining = somethingLongToPrint.Length - i;
if (numberOfCharactersRemaining > 80)
{
tmp = overrun + somethingLongToPrint.Substring(i - overrun.Length, 80);
}
else
{
tmp = overrun + somethingLongToPrint.Substring(somethingLongToPrint.Length - numberOfCharactersRemaining);
}
}
else
{
tmp = somethingLongToPrint.Substring(i, ((80 - overrun.Length)));
}
overrun = print80CharacterLine(tmp);
i += 80;
}
}
// returns what was not printed
private string print80CharacterLine(string src)
{
string eol = "\r\n";
string whatIsLeft = string.Empty;
int index = src.LastIndexOf(" ");
string calculatedString = src.Substring(0, index);
whatIsLeft = src.Substring((index+1));
// print statement here
// do the e.Graphics.DrawString(lines[linesPrinted], someFont, brush, x, y);
textBox1.Text += calculatedString + eol;
return whatIsLeft;
}