按下按钮后将文本调整到固定宽度的列中
Fit text into a fixed-width column after pressing a button
我想将文本框中的文本格式化为 30 到 60 个字符的固定宽度列。将允许用户指定每行的宽度(即列的宽度),但默认为 50 个字符。
我已经做了一些研究,但是我找不到我的目标是什么。
我试过 String.Format
但是,它什么也没做
string test = ""
if(txtBoxContent.Text == string.Empty)
{
MessageBox.Show("There are no '>' symbols to remove. Please paste the infected text first");
}
else if (!txtBoxContent.Text.Contains(">"))
{
MessageBox.Show("There are no '>' symbol(s) in the text OR are already removed");
}
else
{
contentWithoutCharacters = txtBoxContent.Text.Replace(">", "");
txtBoxContent.Text = contentWithoutCharacters;
//MessageBox.Show("Removed Successfully");
test = string.Format("{0,50}", contentWithoutCharacters); // this
}
}
我假设我采用了错误的方法或没有正确使用 string.format
。任何指导将不胜感激
更新:
我想我没有清楚地解释自己。
例如:以下文字为原文
I have text in a text box that I want to format to a fixed-width column of between 30 and 60 characters. The user will be allowed to specify how wide each line should be (i.e. the width of the column) but it will default to 50 characters.
当我按下按钮时,我希望上面的文本每行显示 50 个字符
I have text in a text box that I want to format to a fixed-width new line
column of between 30 and 60 characters. The user will be allowed to specify how new line wide each line should be (i.e. the width of the column) but it will default to 50 characters.
希望现在更有意义
您可以根据需要使用 padleft 或 padright 方法。下面是 padright 方法的例子:
string str = "HelloWorld"
str.PadRight(11);
输出='HelloWorld '
这里是 padleft 方法的例子:
string str = "HelloWorld"
str.PadLeft(11);
输出 =“HelloWorld”
如果您决定将字符串分成 n 宽度的片段,这里有一种方法。但是,您很快就会发现字符串以您可能不希望的方式被分解...这就是为什么您会收到评论,想知道为什么要这样做。
var originalText = "Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! ";
txtBoxContent.Clear();
txtBoxContent.WordWrap = false; //--> to make sure nothing spills to the next line
var part = string.Empty;
var index = 0; //--> define this outside the loop, so you can use it to pick up any remainder
var width = 30; //--> ...or whatever width you need
for ( index = 0; index < ( originalText.Length - width ); index += width )
{
part = originalText.Substring( index, width );
//--> do something useful with the fixed-length part...
txtBoxContent.Append( part + Environment.NewLine );
}
//--> deal with the remainder, if any...
if ( index < originalText.Length )
{
//--> the last piece...it will be less than width wide...
part = originalText.Substring( index, originalText.Length - index );
txtBoxContent.Append( part );
}
...这就是您得到的:
Now is the time; yada yada; th
ing and the thing! Now is the
time; yada yada; thing and the
thing! Now is the time; yada
yada; thing and the thing! Now
is the time; yada yada; thing
and the thing! Now is the tim
e; yada yada; thing and the th
ing! Now is the time; yada yad
a; thing and the thing! Now is
the time; yada yada; thing an
d the thing!
我想将文本框中的文本格式化为 30 到 60 个字符的固定宽度列。将允许用户指定每行的宽度(即列的宽度),但默认为 50 个字符。
我已经做了一些研究,但是我找不到我的目标是什么。
我试过 String.Format
但是,它什么也没做
string test = ""
if(txtBoxContent.Text == string.Empty)
{
MessageBox.Show("There are no '>' symbols to remove. Please paste the infected text first");
}
else if (!txtBoxContent.Text.Contains(">"))
{
MessageBox.Show("There are no '>' symbol(s) in the text OR are already removed");
}
else
{
contentWithoutCharacters = txtBoxContent.Text.Replace(">", "");
txtBoxContent.Text = contentWithoutCharacters;
//MessageBox.Show("Removed Successfully");
test = string.Format("{0,50}", contentWithoutCharacters); // this
}
}
我假设我采用了错误的方法或没有正确使用 string.format
。任何指导将不胜感激
更新: 我想我没有清楚地解释自己。
例如:以下文字为原文
I have text in a text box that I want to format to a fixed-width column of between 30 and 60 characters. The user will be allowed to specify how wide each line should be (i.e. the width of the column) but it will default to 50 characters.
当我按下按钮时,我希望上面的文本每行显示 50 个字符
I have text in a text box that I want to format to a fixed-width new line column of between 30 and 60 characters. The user will be allowed to specify how new line wide each line should be (i.e. the width of the column) but it will default to 50 characters.
希望现在更有意义
您可以根据需要使用 padleft 或 padright 方法。下面是 padright 方法的例子:
string str = "HelloWorld"
str.PadRight(11);
输出='HelloWorld '
这里是 padleft 方法的例子:
string str = "HelloWorld"
str.PadLeft(11);
输出 =“HelloWorld”
如果您决定将字符串分成 n 宽度的片段,这里有一种方法。但是,您很快就会发现字符串以您可能不希望的方式被分解...这就是为什么您会收到评论,想知道为什么要这样做。
var originalText = "Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! ";
txtBoxContent.Clear();
txtBoxContent.WordWrap = false; //--> to make sure nothing spills to the next line
var part = string.Empty;
var index = 0; //--> define this outside the loop, so you can use it to pick up any remainder
var width = 30; //--> ...or whatever width you need
for ( index = 0; index < ( originalText.Length - width ); index += width )
{
part = originalText.Substring( index, width );
//--> do something useful with the fixed-length part...
txtBoxContent.Append( part + Environment.NewLine );
}
//--> deal with the remainder, if any...
if ( index < originalText.Length )
{
//--> the last piece...it will be less than width wide...
part = originalText.Substring( index, originalText.Length - index );
txtBoxContent.Append( part );
}
...这就是您得到的:
Now is the time; yada yada; th
ing and the thing! Now is the
time; yada yada; thing and the
thing! Now is the time; yada
yada; thing and the thing! Now
is the time; yada yada; thing
and the thing! Now is the tim
e; yada yada; thing and the th
ing! Now is the time; yada yad
a; thing and the thing! Now is
the time; yada yada; thing an
d the thing!