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 | | | | re: Insert Spaces
On Nov 21, 8:33 am, shapper <mdmo...@gmail.comwrote: Quote:
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. | | | | 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.WriteLine("{0:00 000 00 00}", i);
Console.ReadLine();
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.com
"shapper" <mdmoura@gmail.comwrote in message
news:14dd1984-529f-4aef-8f27-812a07f3a626@w22g2000yqd.googlegroups.com... Quote:
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
| | | | re: Insert Spaces
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin@gmail.comwrote in
message
news:dc6d5901-2a7b-4565-912d-ce967f3efc4b@41g2000yqf.googlegroups.com... Quote:
On Nov 21, 8:33 am, shapper <mdmo...@gmail.comwrote: Quote:
>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 | | | | re: Insert Spaces
On Nov 21, 4:37*pm, <qwe...@asdf.comwrote: Quote:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac...@gmail.comwrote in
messagenews:dc6d5901-2a7b-4565-912d-ce967f3efc4b@41g2000yqf.googlegroups.com...
> Quote:
On Nov 21, 8:33 am, shapper <mdmo...@gmail.comwrote: > Quote: Quote:
I need to format phone numbers for easier reading:
> Quote: Quote:
961239070 becomes 96 123 90 70
> Quote: Quote:
So a space is introduced always on the places of the example.
> Quote: Quote:
How can I do this? And can I use a Regex?
> > Quote:
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.Substring(7, 2));
Not sure if it is the best way but it is working. | | | | re: Insert Spaces
On Nov 21, 7:19*pm, shapper <mdmo...@gmail.comwrote: Quote:
On Nov 21, 4:37*pm, <qwe...@asdf.comwrote:
>
>
> Quote:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac...@gmail.comwrote in
messagenews:dc6d5901-2a7b-4565-912d-ce967f3efc4b@41g2000yqf.googlegroups.com...
> Quote: Quote:
On Nov 21, 8:33 am, shapper <mdmo...@gmail.comwrote:
>Hello,
> Quote: Quote:
>I need to format phone numbers for easier reading:
> Quote: Quote:
>961239070 becomes 96 123 90 70
> Quote: Quote:
>So a space is introduced always on the places of the example.
> Quote: Quote:
>How can I do this? And can I use a Regex?
> > Quote: Quote:
You can use a regular expression for sure.
> Quote:
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?
> Quote:
I would think string.Format() would be the way to go.
> >
Yes, I am using:
>
phone = string.Format("{0} {1} {2} {3}", phone.Substring(0, 2),
phone.Substring(2, 3), phone.Substring(5, 2),phone.Substring(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 | | | | re: Insert Spaces
On Nov 21, 11:37 am, <qwe...@asdf.comwrote: Quote:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac...@gmail.comwrote in
messagenews:dc6d5901-2a7b-4565-912d-ce967f3efc4b@41g2000yqf.googlegroups.com...
> Quote:
On Nov 21, 8:33 am, shapper <mdmo...@gmail.comwrote: > Quote: Quote:
I need to format phone numbers for easier reading:
> Quote: Quote:
961239070 becomes 96 123 90 70
> Quote: Quote:
So a space is introduced always on the places of the example.
> Quote: Quote:
How can I do this? And can I use a Regex?
> > Quote:
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 | | | | re: Insert Spaces
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin@gmail.comwrote in
message
news:d19f88a6-4da4-43fc-9aaf-df099025b3cd@j38g2000yqa.googlegroups.com... Quote:
On Nov 21, 11:37 am, <qwe...@asdf.comwrote: Quote:
>"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac...@gmail.comwrote in
>messagenews:dc6d5901-2a7b-4565-912d-ce967f3efc4b@41g2000yqf.googlegroups.com...
>> Quote:
On Nov 21, 8:33 am, shapper <mdmo...@gmail.comwrote:
>Hello,
>> Quote:
>I need to format phone numbers for easier reading:
>> Quote:
>961239070 becomes 96 123 90 70
>> Quote:
>So a space is introduced always on the places of the example.
>> Quote:
>How can I do this? And can I use a Regex?
>> >> Quote:
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 |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|