Insert Spaces

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

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
 
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.
 
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.
 
message
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
 
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.
 
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
 
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
 
message
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
 
message


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.

Yes - you can do it with regex.

string s = "961239070";
string s2 = Regex.Replace(s, @"(\d{2})(\d{3})(\d{2})(\d{2})", "$1 $2 $3
$4");

No - I would not do it with regex.

Arne
 
shapper said:
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.

That would MATCH a phone number in that format, but you can't use a regular
expression to REWRITE a string like that.
 
Tim said:
That would MATCH a phone number in that format, but you can't use a regular
expression to REWRITE a string like that.

An example of doing so was posted yesterday.

Arne
 
Back
Top