Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting user defined number of spaces before and after string using C#

I am using string builder to format my string to append and prepend white spaces at the start and end of the string

here is what I have so far:

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'm getting the error incorrect format. What am i doing wrong?

like image 215
user1998735 Avatar asked Nov 17 '16 03:11

user1998735


People also ask

How do you put a space between strings in C?

In C language, we can directly add spaces. printf(“ ”);

How do you put a space at the end of a string?

Use the padEnd() and padStart() methods to add spaces to the end or beginning of a string, e.g. str. padEnd(6, ' '); .

How do you give spaces in C?

For just a space, use ' ' .

How do I add a space in printf?

In the format string, a blank space follows the percent sign, not a type character, so printf will simply display the percent sign. It then displays the blank space (the next character in the format string), and then it displays the backspace character (specified with the escape sequence \b).


4 Answers

I think you need not to use separate operation for formatting the string, you can use .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:- Assume that you need to add Some white spaces(let it be 5) before and after the specific word.

like image 97
sujith karivelil Avatar answered Oct 18 '22 06:10

sujith karivelil


There's two issues here. The first is that you're correctly using a StringBuilder to format a string which reduces the overhead caused by concatenation but you're also performing extra concatenation on that format local variable.

The second issue is that your format string is wrong: it doesn't include the argument index. Your method expects a single word, so that index should be zero before the padding instruction.

Fortunately, you could skip past the concatenation of the format string and simply append your user-defined space (or whatever character) to the fresh instance of the StringBuilder

like image 35
clarkitect Avatar answered Oct 18 '22 06:10

clarkitect


Your code has some errors:

Format Exception will be thrown by this line for sure:

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

Your current format doesn't contain any {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 Format Exception if the textBox2.Text is for an example a11:

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

You need to use int.TryParse

int count = 0;
int.TryParse(textBo2.Text, out count);
like image 3
mybirthname Avatar answered Oct 18 '22 06:10

mybirthname


What seems to be the issue is

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

Letz say if totalChar = 10; than

format = "{-10,10}"

which is not a valid format whereas it should be

{0,10}{1,10}

and thus your string would look like

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

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

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

An example to demostrate your task using 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);
like image 3
Mohit S Avatar answered Oct 18 '22 08:10

Mohit S