Insert Spaces

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

    Insert Spaces

    Hello,

    I need to format phone numbers for easier reading:

    961239070 becomes 96 123 90 70

    So a space is introduced always on the places of the example.

    How can I do this? And can I use a Regex?

    Thanks,
    Miguel
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: Insert Spaces

    On Nov 21, 8:33 am, shapper <mdmo...@gmail. comwrote:
    Hello,
    >
    I need to format phone numbers for easier reading:
    >
    961239070 becomes 96 123 90 70
    >
    So a space is introduced always on the places of the example.
    >
    How can I do this? And can I use a Regex?
    >
    Thanks,
    Miguel
    You can use a regular expression for sure.

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Insert Spaces

      Miguel,

      You could use a regular expression, but assuming that the number is an
      integer, you could do this:

      int i = 961239070;
      Console.WriteLi ne("{0:00 000 00 00}", i);
      Console.ReadLin e();

      You can also us the format string in the static Format method on the
      string class if you are not printing it out to the console.

      If you have the number in a string, you can easily convert it to an int
      or long and then use the format.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "shapper" <mdmoura@gmail. comwrote in message
      news:14dd1984-529f-4aef-8f27-812a07f3a626@w2 2g2000yqd.googl egroups.com...
      Hello,
      >
      I need to format phone numbers for easier reading:
      >
      961239070 becomes 96 123 90 70
      >
      So a space is introduced always on the places of the example.
      >
      How can I do this? And can I use a Regex?
      >
      Thanks,
      Miguel

      Comment

      • Guest's Avatar

        #4
        Re: Insert Spaces


        "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
        message
        news:dc6d5901-2a7b-4565-912d-ce967f3efc4b@41 g2000yqf.google groups.com...
        On Nov 21, 8:33 am, shapper <mdmo...@gmail. comwrote:
        >Hello,
        >>
        >I need to format phone numbers for easier reading:
        >>
        >961239070 becomes 96 123 90 70
        >>
        >So a space is introduced always on the places of the example.
        >>
        >How can I do this? And can I use a Regex?
        >>
        >Thanks,
        >Miguel
        >
        You can use a regular expression for sure.
        How would you format a phone number with a Regex?
        Did you misunderstand the question, or is there really a way to do this with
        Regex?

        I would think string.Format() would be the way to go.

        Bill

        Comment

        • shapper

          #5
          Re: Insert Spaces

          On Nov 21, 4:37 pm, <qwe...@asdf.co mwrote:
          "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac... @gmail.comwrote in
          messagenews:dc6 d5901-2a7b-4565-912d-ce967f3efc4b@41 g2000yqf.google groups.com...
          >
          On Nov 21, 8:33 am, shapper <mdmo...@gmail. comwrote:
          Hello,
          >
          I need to format phone numbers for easier reading:
          >
          961239070 becomes 96 123 90 70
          >
          So a space is introduced always on the places of the example.
          >
          How can I do this? And can I use a Regex?
          >
          Thanks,
          Miguel
          >
          You can use a regular expression for sure.
          >
          How would you format a phone number with a Regex?
          Did you misunderstand the question, or is there really a way to do this with
          Regex?
          >
          I would think string.Format() would be the way to go.
          >
              Bill
          Yes, I am using:

          phone = string.Format(" {0} {1} {2} {3}", phone.Substring (0, 2),
          phone.Substring (2, 3), phone.Substring (5, 2),phone.Substr ing(7, 2));

          Not sure if it is the best way but it is working.

          Comment

          • shapper

            #6
            Re: Insert Spaces

            On Nov 21, 7:19 pm, shapper <mdmo...@gmail. comwrote:
            On Nov 21, 4:37 pm, <qwe...@asdf.co mwrote:
            >
            >
            >
            "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac... @gmail.comwrote in
            messagenews:dc6 d5901-2a7b-4565-912d-ce967f3efc4b@41 g2000yqf.google groups.com...
            >
            On Nov 21, 8:33 am, shapper <mdmo...@gmail. comwrote:
            >Hello,
            >
            >I need to format phone numbers for easier reading:
            >
            >961239070 becomes 96 123 90 70
            >
            >So a space is introduced always on the places of the example.
            >
            >How can I do this? And can I use a Regex?
            >
            >Thanks,
            >Miguel
            >
            You can use a regular expression for sure.
            >
            How would you format a phone number with a Regex?
            Did you misunderstand the question, or is there really a way to do thiswith
            Regex?
            >
            I would think string.Format() would be the way to go.
            >
                Bill
            >
            Yes, I am using:
            >
            phone = string.Format(" {0} {1} {2} {3}", phone.Substring (0, 2),
            phone.Substring (2, 3), phone.Substring (5, 2),phone.Substr ing(7, 2));
            >
            Not sure if it is the best way but it is working.
            Is there a way to mask a string using a Regex expression.

            I think the following regex would work:
            @"^\d{2}\s\d{3} \s\d{2}\s\d{2}$ "

            I am just not sure how to make the mask.

            Thanks,
            Miguel

            Comment

            • Ignacio Machin ( .NET/ C# MVP )

              #7
              Re: Insert Spaces

              On Nov 21, 11:37 am, <qwe...@asdf.co mwrote:
              "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac... @gmail.comwrote in
              messagenews:dc6 d5901-2a7b-4565-912d-ce967f3efc4b@41 g2000yqf.google groups.com...
              >
              On Nov 21, 8:33 am, shapper <mdmo...@gmail. comwrote:
              Hello,
              >
              I need to format phone numbers for easier reading:
              >
              961239070 becomes 96 123 90 70
              >
              So a space is introduced always on the places of the example.
              >
              How can I do this? And can I use a Regex?
              >
              Thanks,
              Miguel
              >
              You can use a regular expression for sure.
              >
              How would you format a phone number with a Regex?
              Did you misunderstand the question, or is there really a way to do this with
              Regex?
              >
              I would think string.Format() would be the way to go.
              >
              Bill
              Hi,

              I assume that the phone number is already as string , if so you have
              to group the characters and then create the new string (by using
              String.Format)
              something like
              Match m = RegEx.Match( "XXXXXXXX", "(\d\d)(\d\d\d) "
              String.Format(" {0} {1} ....", m[0].Value, )

              Please note than the above code is not cmoplete, only meant to serve
              as a guide

              you could also use SubString though

              Comment

              • Bill Butler

                #8
                Re: Insert Spaces

                "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
                message
                news:d19f88a6-4da4-43fc-9aaf-df099025b3cd@j3 8g2000yqa.googl egroups.com...
                On Nov 21, 11:37 am, <qwe...@asdf.co mwrote:
                >"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac... @gmail.comwrote in
                >messagenews:dc 6d5901-2a7b-4565-912d-ce967f3efc4b@41 g2000yqf.google groups.com...
                >>
                On Nov 21, 8:33 am, shapper <mdmo...@gmail. comwrote:
                >Hello,
                >>
                >I need to format phone numbers for easier reading:
                >>
                >961239070 becomes 96 123 90 70
                >>
                >So a space is introduced always on the places of the example.
                >>
                >How can I do this? And can I use a Regex?
                >>
                >Thanks,
                >Miguel
                >>
                You can use a regular expression for sure.
                >>
                >How would you format a phone number with a Regex?
                >Did you misunderstand the question, or is there really a way to do this
                >with
                >Regex?
                >>
                >I would think string.Format() would be the way to go.
                >>
                > Bill
                >
                Hi,
                >
                I assume that the phone number is already as string , if so you have
                to group the characters and then create the new string (by using
                String.Format)
                something like
                Match m = RegEx.Match( "XXXXXXXX", "(\d\d)(\d\d\d) "
                String.Format(" {0} {1} ....", m[0].Value, )
                >
                Please note than the above code is not cmoplete, only meant to serve
                as a guide
                >
                you could also use SubString though
                Got it.
                I thought that you were saying that Regex could be used as some sort of mask
                to format the display.
                Absolutely, you could use it to extract the component data from the input
                string.
                I had assumed SubString() for the component extraction.

                Thanks
                Bill

                Comment

                Working...