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?

1
  • 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 Commented Mar 9, 2010 at 18:22

10 Answers 10

19

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);
Sign up to request clarification or add additional context in comments.

3 Comments

@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.
@Danny - You were right, it's definitely a valid point and someone may come across this trying to do just that later on.
wouldn't it be easier and more efficient to use the Pad method mentioned below?
8
int i=8;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(" ", i);

4 Comments

Not the "slickest" answer, but gets a +1 for straightforwardness.
It is the slickest and the correct answer, he is using the stringbuilder class, strings are immutable. msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx
@Joe - yep, strings are immutable, what of it?
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
6
new string(' ', NumberOfTabs )

1 Comment

Going on the OP's question, it seems like it should be NumberOfTabs * 4
4
str = str.PadLeft(str.Length+tabs*4);

Comments

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

3 Comments

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.
For small number of concatenations, StringBuilder has more overhead than it saves.
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.
2

You could use something like this:

String.Empty.PadRight(NumberOfTabs)

Comments

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#)

Comments

2
int NumberOfTabs = 2;
string line = "my line";
string results = line.PadLeft(line.Length + NumberOfTabs, ' ');

Comments

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

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.