To simply add spaces or characters, front and / or back, Padding will work just fine.
private void button1_Click(object sender, EventArgs e) { int amount; int.TryParse(textBox2.Text, out amount); var str = textBox1.Text.PadLeft(amount + textBox1.TextLength); str = str.PadRight(amount + str.Length); textBox3.Text = str; }
Then you can select spacer (paddingChar) also later, if necessary / required
var str = textBox1.Text.PadLeft(amount + textBox1.TextLength, '>'); str = str.PadRight(amount + str.Length, '<');
Additionally with an additional method:
private void button1_Click(object sender, EventArgs e) { textBox3.Text = Format(textBox1.Text, textBox2.Text); } private string Format(string word, string spaces) { int amount; int.TryParse(spaces, out amount); var str = word.PadLeft(amount + word.Length); str = str.PadRight(amount + str.Length); return str; }
source share