Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. string formate

string formate

Scheduled Pinned Locked Moved C#
14 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jashimu
    wrote on last edited by
    #1

    Hi all, I need to format string and having some problems. when user enter into a textbox i.e "12345678". I need to format this input into this. "12 345 678". I am doing this way but not working

    string p = TextBox.Text.Trim();
    string.Format("{0},{1},{2}", p.Substring(0,2), p.Substring(2,4), p.Substring(5,7));

    E M A P L 5 Replies Last reply
    0
    • J jashimu

      Hi all, I need to format string and having some problems. when user enter into a textbox i.e "12345678". I need to format this input into this. "12 345 678". I am doing this way but not working

      string p = TextBox.Text.Trim();
      string.Format("{0},{1},{2}", p.Substring(0,2), p.Substring(2,4), p.Substring(5,7));

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      You should probably remove the comma's from your Format string. The rest is just about adding up your substring index and length values correctly.

      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

      1 Reply Last reply
      0
      • J jashimu

        Hi all, I need to format string and having some problems. when user enter into a textbox i.e "12345678". I need to format this input into this. "12 345 678". I am doing this way but not working

        string p = TextBox.Text.Trim();
        string.Format("{0},{1},{2}", p.Substring(0,2), p.Substring(2,4), p.Substring(5,7));

        M Offline
        M Offline
        mabrahao
        wrote on last edited by
        #3

        You should replace commas to spaces and String.Format is uppercase in the S

        A 1 Reply Last reply
        0
        • J jashimu

          Hi all, I need to format string and having some problems. when user enter into a textbox i.e "12345678". I need to format this input into this. "12 345 678". I am doing this way but not working

          string p = TextBox.Text.Trim();
          string.Format("{0},{1},{2}", p.Substring(0,2), p.Substring(2,4), p.Substring(5,7));

          A Offline
          A Offline
          AspDotNetDev
          wrote on last edited by
          #4

          Try this:

          string p = TextBox.Text.Trim();
          p = string.Format("{0} {1} {2}", p.Substring(0, 2), p.Substring(2, 3), p.Substring(5, 3));
          // If you are using Windows Forms, this will show the output. You will do it differently if you are using ASP.NET.
          MessageBox.Show(p);

          Here is what was wrong with your code:

          • Your sample output contains spaces, but your format strong contains commas.
          • string.Format returns the modified string, yet you were not assigning the return value to anything.
          • string.Substring has 2 parameters. The second prameter is the string length, not a second index. So where you used 4 and 7, you should have used 3.

          [Managing Your JavaScript Library in ASP.NET]

          J 1 Reply Last reply
          0
          • M mabrahao

            You should replace commas to spaces and String.Format is uppercase in the S

            A Offline
            A Offline
            AspDotNetDev
            wrote on last edited by
            #5

            mabrahao wrote:

            String.Format is uppercase in the S

            Actually, both string and String will work.

            [Managing Your JavaScript Library in ASP.NET]

            1 Reply Last reply
            0
            • J jashimu

              Hi all, I need to format string and having some problems. when user enter into a textbox i.e "12345678". I need to format this input into this. "12 345 678". I am doing this way but not working

              string p = TextBox.Text.Trim();
              string.Format("{0},{1},{2}", p.Substring(0,2), p.Substring(2,4), p.Substring(5,7));

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              One thing you should know - the second parameter in Substring is not the position to end on, it's the length of the substring to extract. It goes Substring(index, length);

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              A 1 Reply Last reply
              0
              • A AspDotNetDev

                Try this:

                string p = TextBox.Text.Trim();
                p = string.Format("{0} {1} {2}", p.Substring(0, 2), p.Substring(2, 3), p.Substring(5, 3));
                // If you are using Windows Forms, this will show the output. You will do it differently if you are using ASP.NET.
                MessageBox.Show(p);

                Here is what was wrong with your code:

                • Your sample output contains spaces, but your format strong contains commas.
                • string.Format returns the modified string, yet you were not assigning the return value to anything.
                • string.Substring has 2 parameters. The second prameter is the string length, not a second index. So where you used 4 and 7, you should have used 3.

                [Managing Your JavaScript Library in ASP.NET]

                J Offline
                J Offline
                jashimu
                wrote on last edited by
                #7

                Thanks for your reply. it is working for me.

                1 Reply Last reply
                0
                • P Pete OHanlon

                  One thing you should know - the second parameter in Substring is not the position to end on, it's the length of the substring to extract. It goes Substring(index, length);

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #8

                  Repost. :)

                  [Managing Your JavaScript Library in ASP.NET]

                  1 Reply Last reply
                  0
                  • J jashimu

                    Hi all, I need to format string and having some problems. when user enter into a textbox i.e "12345678". I need to format this input into this. "12 345 678". I am doing this way but not working

                    string p = TextBox.Text.Trim();
                    string.Format("{0},{1},{2}", p.Substring(0,2), p.Substring(2,4), p.Substring(5,7));

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    You should NOT do this with string manipulations; it would go completely wrong if the user types strange things, such as the intended number prefixed with a lot of zeroes. The proper way to do this consists of two steps: 1. parse the user input, i.e. turn his characters into an actual number. The simplest way would be:

                    int val;
                    bool OK=int.TryParse(myTextBox.Text, out val);

                    Whatever the user does wrong (e.g. typing letters, or providing no input at all) will result in OK being false; if the input is acceptable, OK will be true and val will contain its value. 2. format the number in the way you want it. A simple way to get two spaces in a number assumed to require 8 digits is:

                    string s=string.Format("{0:## ### ###.##}", val);

                    Assuming val=12345678 the result will be 12 345 678 :)

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                    Please use <PRE> tags for code snippets, they improve readability.
                    CP Vanity has been updated to V2.3

                    A L 2 Replies Last reply
                    0
                    • L Luc Pattyn

                      You should NOT do this with string manipulations; it would go completely wrong if the user types strange things, such as the intended number prefixed with a lot of zeroes. The proper way to do this consists of two steps: 1. parse the user input, i.e. turn his characters into an actual number. The simplest way would be:

                      int val;
                      bool OK=int.TryParse(myTextBox.Text, out val);

                      Whatever the user does wrong (e.g. typing letters, or providing no input at all) will result in OK being false; if the input is acceptable, OK will be true and val will contain its value. 2. format the number in the way you want it. A simple way to get two spaces in a number assumed to require 8 digits is:

                      string s=string.Format("{0:## ### ###.##}", val);

                      Assuming val=12345678 the result will be 12 345 678 :)

                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                      Please use <PRE> tags for code snippets, they improve readability.
                      CP Vanity has been updated to V2.3

                      A Offline
                      A Offline
                      AmbiguousName
                      wrote on last edited by
                      #10

                      I always like your answers.

                      1 Reply Last reply
                      0
                      • L Luc Pattyn

                        You should NOT do this with string manipulations; it would go completely wrong if the user types strange things, such as the intended number prefixed with a lot of zeroes. The proper way to do this consists of two steps: 1. parse the user input, i.e. turn his characters into an actual number. The simplest way would be:

                        int val;
                        bool OK=int.TryParse(myTextBox.Text, out val);

                        Whatever the user does wrong (e.g. typing letters, or providing no input at all) will result in OK being false; if the input is acceptable, OK will be true and val will contain its value. 2. format the number in the way you want it. A simple way to get two spaces in a number assumed to require 8 digits is:

                        string s=string.Format("{0:## ### ###.##}", val);

                        Assuming val=12345678 the result will be 12 345 678 :)

                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                        The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                        Please use <PRE> tags for code snippets, they improve readability.
                        CP Vanity has been updated to V2.3

                        L Offline
                        L Offline
                        Lutoslaw
                        wrote on last edited by
                        #11

                        Could be also 1. If the currect culture uses ' ' as a group separator:

                        string.Format("{0:n0}", val)

                        2. If it doesn't:

                        var provider = (CultureInfo) CultureInfo.CurrentCulture.Clone();
                        provider.NumberFormat.NumberGroupSeparator = " ";
                        string.Format(provider, "{0:n0}", val);

                        Greetings - Jacek

                        L 1 Reply Last reply
                        0
                        • L Lutoslaw

                          Could be also 1. If the currect culture uses ' ' as a group separator:

                          string.Format("{0:n0}", val)

                          2. If it doesn't:

                          var provider = (CultureInfo) CultureInfo.CurrentCulture.Clone();
                          provider.NumberFormat.NumberGroupSeparator = " ";
                          string.Format(provider, "{0:n0}", val);

                          Greetings - Jacek

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          yes, there are alternatives for each of both steps. Having the two steps was the essence in my message. :)

                          Luc Pattyn [My Articles] Nil Volentibus Arduum

                          The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                          Please use <PRE> tags for code snippets, they improve readability.
                          CP Vanity has been updated to V2.4

                          L 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            yes, there are alternatives for each of both steps. Having the two steps was the essence in my message. :)

                            Luc Pattyn [My Articles] Nil Volentibus Arduum

                            The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                            Please use <PRE> tags for code snippets, they improve readability.
                            CP Vanity has been updated to V2.4

                            L Offline
                            L Offline
                            Lutoslaw
                            wrote on last edited by
                            #13

                            To be precise: My (1) and (2) were alternatives to your (2). They both assumes that your (1) was already done.

                            Greetings - Jacek

                            L 1 Reply Last reply
                            0
                            • L Lutoslaw

                              To be precise: My (1) and (2) were alternatives to your (2). They both assumes that your (1) was already done.

                              Greetings - Jacek

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #14

                              Of course; and that is how I understood it. :)

                              Luc Pattyn [My Articles] Nil Volentibus Arduum

                              The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                              Please use <PRE> tags for code snippets, they improve readability.
                              CP Vanity has been updated to V2.4

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups