use*_*735 6 .net c# string textbox string-formatting
我正在使用字符串构建器来格式化我的字符串以追加并在字符串的开头和结尾添加前面的空格
这是我到目前为止:
private void button1_Click(object sender, EventArgs e)
{
String Word = textBox1.Text;
AppendPrependText(Word);
}
private void AppendPrependText (String Word)
{
int count = Convert.ToInt32(textBox2.Text);
int WordCount = Word.Count();
int totalChar = count + WordCount;
string format = "{-"+totalChar+ "," +totalChar+ "}";
StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Format(format, Word));
textBox3.Text = sb.ToString();
}
Run Code Online (Sandbox Code Playgroud)
但我得到的错误格式不正确.我究竟做错了什么?
我认为您不需要使用单独的操作来格式化字符串,您可以.AppendFormat()使用StringBuilder Class. 这是给您的示例代码:
StringBuilder sbAppendFormat = new StringBuilder();
int numberOfSpaces=0;
if(int.TryParse(textBo2.Text, out numberOfSpaces))
{
string whiteSpaceSequence= new string(' ',numberOfSpaces);
sbAppendFormat.AppendFormat("{0}{1}{0}", whiteSpaceSequence, "This is your String");
}
textBox3.Text = sbAppendFormat.ToString();
Run Code Online (Sandbox Code Playgroud)
5注意:- 假设您需要在特定单词之前和之后添加一些空格(让它成为)。
| 归档时间: |
|
| 查看次数: |
202 次 |
| 最近记录: |