Add "spaces" to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gdjoshua

    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...
  • Paul E Collins

    #2
    Re: Add "spaces&qu ot; to string

    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.


    Comment

    • Carl Daniel [VC++ MVP]

      #3
      Re: Add "spaces&qu ot; to string

      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



      Comment

      • RSH

        #4
        Re: Add "spaces&qu ot; to string

        I personally would use a Stringbuilder object:

        sb as new StringBuilder() ;

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

        lblDisplay.Text = sb.ToString();


        "gdjoshua" <gdjoshua@discu ssions.microsof t.comwrote in message
        news:DE628C15-E9B1-4DB8-9107-32943B5A19CA@mi crosoft.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...

        Comment

        • Marc Gravell

          #5
          Re: Add &quot;spaces&qu ot; to string

          For this? not sure if that is worthwhile... Jon Skeet puts it far better
          than I could...



          Marc


          Comment

          • Mythran

            #6
            Re: Add &quot;spaces&qu ot; to string


            "Marc Gravell" <marc.gravell@g mail.comwrote in message
            news:%236t6KRLt GHA.2260@TK2MSF TNGP02.phx.gbl. ..
            For this? not sure if that is worthwhile... Jon Skeet puts it far better
            than I could...
            >

            >
            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


            Comment

            • Code Monkey

              #7
              Re: Add &quot;spaces&qu ot; to string

              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" <marc.gravell@g mail.comwrote in message
              news:%236t6KRLt GHA.2260@TK2MSF TNGP02.phx.gbl. ..
              For this? not sure if that is worthwhile... Jon Skeet puts it far better
              than I could...



              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

              Comment

              Working...