Chevy Mark Sunderland
Chevy Mark Sunderland

Reputation: 435

How to add space in string in c#

I am using c sharp

I want to add some space suffix and prefix in my string Advertising

I have tried this without success

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string txt = "Advertising";
        string padrightleft = txt.ToString().PadRight(10, ' ').PadLeft(10, ' ').ToUpper();

        Response.Write(padrightleft.ToString());
    }
}

Upvotes: 0

Views: 4080

Answers (1)

Sean
Sean

Reputation: 62492

PadLeft and PadRight will only add padding it the string is shorter that then number of padding characters you want to add. Since Advertising is 11 characters long and you are padding to 10 characters the string won't change.

If you just want to add a prefix and suffix to a piece of string then you can do this:

string txt = "Advertising";

var padding = new string(' ', 10);
var newText = string.Concat(padding, txt, padding);

Upvotes: 3

Related Questions