By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,025 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Insert Spaces

P: n/a
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
Nov 21 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.
Nov 21 '08 #2

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"shapper" <md*****@gmail.comwrote in message
news:14**********************************@w22g2000 yqd.googlegroups.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

Nov 21 '08 #3

P: n/a

"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:dc**********************************@41g2000y qf.googlegroups.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

Nov 21 '08 #4

P: n/a
On Nov 21, 4:37*pm, <qwe...@asdf.comwrote:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac...@gmail.comwrote in
messagenews:dc**********************************@4 1g2000yqf.googlegroups.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.Substring(7, 2));

Not sure if it is the best way but it is working.
Nov 21 '08 #5

P: n/a
On Nov 21, 7:19*pm, shapper <mdmo...@gmail.comwrote:
On Nov 21, 4:37*pm, <qwe...@asdf.comwrote:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac...@gmail.comwrote in
messagenews:dc**********************************@4 1g2000yqf.googlegroups.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.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
Nov 21 '08 #6

P: n/a
On Nov 21, 11:37 am, <qwe...@asdf.comwrote:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac...@gmail.comwrote in
messagenews:dc**********************************@4 1g2000yqf.googlegroups.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
Nov 21 '08 #7

P: n/a
"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:d1**********************************@j38g2000 yqa.googlegroups.com...
On Nov 21, 11:37 am, <qwe...@asdf.comwrote:
>"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac...@gmail.comwrote in
messagenews:dc**********************************@ 41g2000yqf.googlegroups.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

Nov 22 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.