Add "spaces" to string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

New to C##.. All I want to do is add spaces to a string I'm displaying in
label? Anybody know the easiest way to do this. char(32) doesn't seem to be
working...
 
gdjoshua said:
New to C##.. All I want to do is add spaces
to a string I'm displaying in label? Anybody
know the easiest way to do this.
char(32) doesn't seem to be working...

You can add one string to another.

string s = "hello";
s = " " + s; // now it's " hello"
s += " "; // now it's " hello "

For a label, you could do this directly to the Text property.

Eq.
 
gdjoshua said:
New to C##.. All I want to do is add spaces to a string I'm
displaying in label? Anybody know the easiest way to do this.
char(32) doesn't seem to be working...

..NET strings are immutable - you can't change them. What you can do is
create a new string that is based on an existing string.

Something along the lines of:

string str = "abcd";
str = str.Insert(2," ");
// str is now "ab cd"

Is that what you were looking for? If not, please post a bit of code that
shows what you're trying and someone will be able to help you.

-cd
 
I personally would use a Stringbuilder object:

sb as new StringBuilder();

sb.Append("Test");
sb.Append(" String");

lblDisplay.Text = sb.ToString();
 
how about the following - using the PadLeft method?


public string s()
{
string str = "test";
str = str.PadLeft(3, ' ');
return str;
}

just an idea!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top