Insert user-defined number of spaces before and after a line using C #

I use a line builder to format my line to add and add white spaces at the beginning and end of a line

here is what i still have:

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(); } 

but I get the wrong format of the error. What am I doing wrong?

+6
source share
6 answers

I think you do not need to use a separate operation to format the string, you can use the .AppendFormat() method of the StringBuilder Class . Here is a sample code for you:

 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(); 

Note. - Suppose you need to add some white spaces (let it be 5 ) before and after a certain word.

+4
source

There are two questions. First, you use StringBuilder to format the string, which reduces the overhead caused by concatenation, but you also do additional concatenation in the local variable format .

The second problem is that your format string is incorrect: it does not include the argument index. Your method expects a single word, so the index must be zero before the fill command.

Fortunately, you can skip the concatenation of a format string and simply add your user space (or any other character) to a new instance of StringBuilder

+3
source

There are some errors in your code:

Format Exception will be precisely set by this line:

 sb.AppendLine(string.Format(format, Word)); 

Your current format does not contain {0} in which the Word value should be replaced.

 //you should put here somewhere {0} in the format or remove the Word for string.Format //for an example string format = "{-" + totalChar + "," + totalChar + "}{0}"; 

Also this line is possible with a Format Exception if textBox2.Text for example a11:

 int count = Convert.ToInt32(textBox2.Text); 

You need to use int.TryParse

 int count = 0; int.TryParse(textBo2.Text, out count); 
+3
source

What is the problem:

 string format = "{-"+totalChar+ "," +totalChar+ "}"; 

Let them say if totalChar = 10; than

 format = "{-10,10}" 

which is not a valid format, whereas it should be

 {0,10}{1,10} 

and therefore your line will look like

 Console.WriteLine(string.Format("{0,10}{1,10}", "Mohit","")); 

The third argument was intentionally left blank so that nothing is printed after the word. but you will have 10 spaces.

But I would recommend you use String.PadRight and String.PadLeft .

An example of demonstrating your task with PadLeft and PadRight

 int count = 5; string st = "mohit "; int WordCount = st.Count(); int totalChar = count + WordCount; st = st.PadRight(totalChar, ' '); st = st.PadLeft(totalChar + count, ' '); Console.WriteLine(st); 
+3
source

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; } 
+3
source

I did not use StringBuilder , I returned String from AppendPrependText . The if statement checks for invalid integer input in textBox2 if its invalid returns the original string. If this is a valid integer, create a padString space with count , then return the original string placed between the two of the padStrings .

EDIT: Added checking for negative numbers by adding AND count > 0 to the if statement.

 private String AppendPrependText(String Word) { int count = 0; if (int.TryParse(textBox2.Text, out count) && count > 0) { String padString = "".PadLeft(count, ' '); return padString + Word.ToString() + padString; } else { return Word; } } private void button1_Click_1(object sender, EventArgs e) { String Word = textBox1.Text; textBox3.Text = ">" + AppendPrependText(Word) + "<"; } 
+1
source

Source: https://habr.com/ru/post/1012395/


All Articles