我在文本框中输入文本,我希望将其格式化为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
。任何指导将不胜感激
更新: 我想我没有清楚地说明自己。
例如:下面的文本是原始文本
我在文本框中输入文本,我希望将其格式化为30到60个字符的固定宽度的列。允许用户指定每行的宽度(即列的宽度),但默认为50个字符。
当我按下按钮时,我希望以上文本在每行上显示50个字符
我在文本框中输入了要格式化为固定宽度的新行的文本 每列30到60个字符。允许用户指定每行换行的宽度(即列的宽度),但默认为50个字符。
希望现在更有意义
答案 0 :(得分:0)
您可以根据需要使用padleft或padright方法。这是padright方法的示例:
string str = "HelloWorld"
str.PadLeft(11);
输出='HelloWorld'
这是padleft方法的示例:
'textbox2 = {\"}
'textbox3 = {\"}
Dim sSource As String = RichTextBox1.Text 'String that is being searched
Dim sDelimStart As String = TextBox2.Text 'First delimiting word
Dim sDelimEnd As String = TextBox3.Text 'Second delimiting word
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd, nIndexStart + sDelimStart.Length + 1) 'Find the first occurrence of f2
If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
MessageBox.Show(res) 'Display
Else
MessageBox.Show("One or both of the delimiting words were not found!")
End If
输出='HelloWorld'
答案 1 :(得分:0)
如果您决定将字符串分解为 n 个宽度的片段,请采用以下方法。但是,您很快就会发现该字符串以您可能不希望使用的方式分解了...这就是为什么您收到有关为什么要这样做的评论的原因。
@Component({
...
encapsulation: ViewEncapsulation.None
})
...这就是你得到的:
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 );
}