471,065 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,065 software developers and data experts.

Add "spaces" to string

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...
Jul 31 '06 #1
6 52669
gdjoshua wrote:
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.
Jul 31 '06 #2
gdjoshua wrote:
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

Jul 31 '06 #3
RSH
I personally would use a Stringbuilder object:

sb as new StringBuilder();

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

lblDisplay.Text = sb.ToString();
"gdjoshua" <gd******@discussions.microsoft.comwrote in message
news:DE**********************************@microsof t.com...
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...

Jul 31 '06 #4
For this? not sure if that is worthwhile... Jon Skeet puts it far better
than I could...

http://www.yoda.arachsys.com/csharp/stringbuilder.html

Marc
Jul 31 '06 #5

"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
For this? not sure if that is worthwhile... Jon Skeet puts it far better
than I could...

http://www.yoda.arachsys.com/csharp/stringbuilder.html

Marc
Jon Skeet puts *most* coding stuff far better than I could :P That's why he
probably gets more $$$ than me :D But can he play games like I can ... hmm

:-P

Mythran
Jul 31 '06 #6
how about the following - using the PadLeft method?
public string s()
{
string str = "test";
str = str.PadLeft(3, ' ');
return str;
}

just an idea!
Mythran wrote:
"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
For this? not sure if that is worthwhile... Jon Skeet puts it far better
than I could...

http://www.yoda.arachsys.com/csharp/stringbuilder.html

Marc

Jon Skeet puts *most* coding stuff far better than I could :P That's why he
probably gets more $$$ than me :D But can he play games like I can ... hmm

:-P

Mythran
Jul 31 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by \Dandy\ Randy | last post: by
8 posts views Thread by Mark | last post: by
5 posts views Thread by Greg Collins [InfoPath MVP] | last post: by
5 posts views Thread by Stijn VA | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.