2

I know this is a very stupid and elementary question, but I really have no idea on other ways to accomplish this.

Now I know I can insert spaces within the text like my example below.

int num = some numerical value;
status.Text = "Successfully Deleted"+ " " + num + " " + "Files";

However, I'm wondering is there a neater way to do this. I always done it this way, but I'm wondering whether there's an easier/neater way to it. Thanks.

0

2 Answers 2

10

Using String class:

int num = 5;
status.Text = String.Format("Successfully Deleted {0} Files", num);

Using StringBuilder class:

int num = 5;
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Successfully Deleted {0} Files", num);
status.Text = sb.ToString();
Sign up to request clarification or add additional context in comments.

5 Comments

status.Text = String.Format("Successfully Deleted {0} File{1}", num, (num>1)?"s":"");
Wow, that was easy, thanks. I faintly remembered it had something to do with the {0}. Thanks.
@Movieboy if this answer helped you - don't be shy to accept it stackoverflow.com/faq#howtoask
Why use a stringbuilder here? It's not adding anything.
For the sake of different flavors.
0

More options:

status.Text = "Successfully Deleted " + num + " Files";

Or:

public static string SurroundWithSpaces(this object o)
{
    return " " + o + " ";
}
status.Text = "Successfully Deleted" + num.SurroundWithSpaces() + "Files";

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.