7

I have a string say message, whose length is not variable. But whatever the length is, I have to add 35 spaces to it before starting another method.

Suggestions Please?

Thanks!

Ex - String = "abc" , should become "abc" + 35 spaces;

No matter what the string is, I need to "Append" 35 spaces to the end of the string.

2
  • 2
    Do you need to add 35 spaces or make sure the result is 35 characters wide? Adding x number of spaces is an odd requirement... Commented Nov 2, 2012 at 20:09
  • @Stanley - Yup, I meant appending 35 spaces and not trimming the string to a specific length Commented Nov 2, 2012 at 20:14

4 Answers 4

16

This should do the trick:

message = message.PadRight(message.Length + 35, ' ');
Sign up to request clarification or add additional context in comments.

Comments

6
string s = "abc";
s += new string(' ', 35);

Comments

3
string paddedValue = string.Format("ABC{0}", new String(" ", 35));

Comments

3

In order to pad a string in C# and VB.net, can use the PadRight method of the String object:

It has two overloads:

String.PadRight(Int32 NumOfChars)
String.PadRight(Int32 NumOfChars, char Char)

F.ex:

string myString = "abc".PadRight(numOfChars, charToPadWith);

or

myString = myString.PadRight(numOfChars, charToPadWith);

For documentation:
http://msdn.microsoft.com/en-us/library/system.string.padright.aspx

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.