6

I have a string, and I want to add a number of spaces to the beginning of that string based on an int variable.
I want to do something like this:

int NumberOfTabs = 2;
string line = "my line";
string line = String.Format("{0}{1}", "    " * NumberOfTabs, line);

...and now line would have 8 spaces

What is the easiest way to do this?

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
NotDan
  • 31,709
  • 36
  • 116
  • 156
  • 1
    Although not the question you asked, string concatenation with `+` is clearer, simpler and (if it matters) faster than `string.Format` for simple cases, i.e. `new string(' ', NumberOfTabs) + line` – Daniel Earwicker Mar 09 '10 at 18:22

10 Answers10

18

You can use the String(char, Int32) constructor like this:

string line = String.Format("{0}{1}", new String(' ', NumberofTabs * 4), line);

or a bit more efficient:

string line = String.Concat(new String(' ', NumberofTabs * 4), line);

or, a bit more concise :)

string line = new String(' ', NumberofTabs * 4).Concat(line);

A comment made a good point, if you want to actually have the tab character, just change the ' ' to '\t' and take out the * 4 like this:

string line = String.Concat(new String('\t', NumberofTabs), line);
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • @Danny - That's what the original posted had, maybe code indentation? In any case you can do `'\t'` instead of `' '` and take off the `* 4` if want true tabs. – Nick Craver Mar 09 '10 at 18:25
  • @Danny - You were right, it's definitely a valid point and someone may come across this trying to do just that later on. – Nick Craver Mar 09 '10 at 18:28
  • 1
    wouldn't it be easier and more efficient to use the Pad method mentioned below? – CodeMonkey1313 Mar 09 '10 at 18:33
8
int i=8;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(" ", i);
Chris McCall
  • 10,317
  • 8
  • 49
  • 80
  • 1
    Not the "slickest" answer, but gets a +1 for straightforwardness. – Beska Mar 09 '10 at 18:25
  • It is the slickest and the correct answer, he is using the stringbuilder class, strings are immutable. http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx – Joe Pitz Mar 09 '10 at 18:31
  • @Joe - yep, strings are immutable, what of it? – Daniel Earwicker Mar 09 '10 at 19:08
  • All of the above examples are using string variables, Every time you concat you create a new string. According to Microsoft the best way to handle this is using stringbuilder class – Joe Pitz Mar 09 '10 at 19:19
6
new string(' ', NumberOfTabs )
Codism
  • 5,928
  • 6
  • 28
  • 29
4
str = str.PadLeft(str.Length+tabs*4);
ANeves
  • 6,219
  • 3
  • 39
  • 63
3

In C# strings are immutable. You should really use the stringbuilder class.

Code examples are listed in the link:

http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx

Joe Pitz
  • 2,434
  • 3
  • 25
  • 30
  • Yes it does, every time you concat a string you create a new un-needed string. Use of stringbuilder prevents this and is the recommended way to increase the length of the string, which the op is trying to perform. – Joe Pitz Mar 09 '10 at 19:21
  • For small number of concatenations, StringBuilder has more overhead than it saves. – Mike Polen Mar 09 '10 at 19:35
  • If you are just looking at concating strings, yes. But if you look at garage collection, maybe not. Garbage collection is very expensive. More extra strings, means more garbage collection. – Joe Pitz Mar 09 '10 at 20:00
2

You could use something like this:

String.Empty.PadRight(NumberOfTabs)
Ben Griswold
  • 17,793
  • 14
  • 58
  • 60
2

You can add tabs at the beginning of your text like this:

line.PadLeft(NumberOfTabs, '\t');

\t being the escape character for "tab" (Inserting a tab character into text using C#)

Community
  • 1
  • 1
Danny T.
  • 1,130
  • 10
  • 23
2
int NumberOfTabs = 2;
string line = "my line";
string results = line.PadLeft(line.Length + NumberOfTabs, ' ');
Tom Brothers
  • 5,929
  • 1
  • 20
  • 17
2

Not the best answer by any measure, but here's an amusing one, a little LINQ one-liner:

var result = new string(Enumerable.Repeat(' ', count).Concat("my line").ToArray());
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
0

You can create a new string containing a custom number of spaces. Unfortunately, there's no string multiplication like in Python (" " * 2). But you can multiply the number of spaces by 4 to get "tabs":

int numberOfTabs = 2;
string line = "my line";
string whitespaces = new string(' ', numberOfTabs * 4);
string s = whitespaces + line;
AndiDog
  • 68,631
  • 21
  • 159
  • 205