string formatting

  • Thread starter Thread starter ilona
  • Start date Start date
I

ilona

Hi all,
I store phone numbers in the database as 123447775665554(input mask is used
for input, and some numbers have extensions), and I also know from db if the
number is Canadian, US, or some other country. When I retrieve the phone
numbers, I need to display them as (###) ###-#### x 99999 if it is a
Canadian number or (###) ###-#### Ext. 99999 if it is US phone number.
Potentially I'd have other countries added as well which might have
completely different formatting. Input Mask and format for output formatting
will be stored in db, per country.

How would you approach formatting the output in this case?
 
Easiest solution, taking into account the fact that you want to formatter
stored in the database, would be to use a regular expression... Store that in
the database and then in your code just do a Regex.Replace.

Brian
 
I tried this function, but it returns unformatted string. My extension is
optional - am I doing smth wrong?
private static string Filter(string userInput)

{

System.Text.RegularExpressions.Regex re = new
System.Text.RegularExpressions.Regex("\\(\\d{3}\\) \\d{3}-\\d{4}
Ext.\\d{5}");

string filtered = re.Replace(userInput,"\\(\\d{3}\\) \\d{3}-\\d{4}
Ext.\\d{5}");

return filtered;

}
 
You need something like this (just one line)

string result = Regex.Replace(input, @"(\d{3})(\d{3})(\d{4})(\d{5})", "($1)
$2-$3 Ext. $4");

example input:
"123447775665554"

output:
"(123) 447-7756 Ext. 65554"


Each $ in the second string represents the contents of a group of ( and ) in
the regex in the first part.

Hope this helps.
 
You need something like this (just one line)

string result = Regex.Replace(input, @"(\d{3})(\d{3})(\d{4})(\d{5})", "($1)
$2-$3 Ext. $4");

example input:
"123447775665554"

output:
"(123) 447-7756 Ext. 65554"


Each $ in the second string represents the contents of a group of ( and ) in
the regex in the first part.

Hope this helps.
 
Thank you, Brian.
Is there a way to accomplish this with just a regular expression without
having to match parts of the string? If I am to store regular expression in
the database, it would not be generic if I have to determine the parts of
the string to match.
 
I'm not sure I follow.... If you are storing the "Formatter" in the database
for each country then you could store the regular expression and regular
expression replace string in a single filed in the database.

For example, for US number the regex and replace string I gave you should
work (If I followed your formatting rules correctly) and you could store
something like this in the database:

"(\d{3})(\d{3})(\d{4})(\d{5})^($1) $2-$3 Ext. $4"

notice that I have used a ^ as a deliminator. When you want to get the
formatter for the US back from the database you will retrieve this string and
then do something like this:

string[] formatter = stringFromDb.Split('^')
string result = Regex.Replace(input, formatter[0], formatter[1]);

And for Canadian numbers you could have the following stored in the DB:

"(\d{3})(\d{3})(\d{4})(\d{5})^($1) $2-$3 x $4"

I hope I'm explaining that correctly.
 
This worked perfectly! Thanks a million! I added "?" after extension in the
replace string to make the extension an optional input:
result=FormatOutput(phone,@"(\d{3})(\d{3})(\d{4})(\d{5})?^($1) $2-$3 Ext.
$4");

I wonder what would be the syntax to make the matching part of extension
"Ext. $4" optional



Brian Delahunty said:
I'm not sure I follow.... If you are storing the "Formatter" in the database
for each country then you could store the regular expression and regular
expression replace string in a single filed in the database.

For example, for US number the regex and replace string I gave you should
work (If I followed your formatting rules correctly) and you could store
something like this in the database:

"(\d{3})(\d{3})(\d{4})(\d{5})^($1) $2-$3 Ext. $4"

notice that I have used a ^ as a deliminator. When you want to get the
formatter for the US back from the database you will retrieve this string and
then do something like this:

string[] formatter = stringFromDb.Split('^')
string result = Regex.Replace(input, formatter[0], formatter[1]);

And for Canadian numbers you could have the following stored in the DB:

"(\d{3})(\d{3})(\d{4})(\d{5})^($1) $2-$3 x $4"

I hope I'm explaining that correctly.



ilona said:
Thank you, Brian.
Is there a way to accomplish this with just a regular expression without
having to match parts of the string? If I am to store regular expression in
the database, it would not be generic if I have to determine the parts of
the string to match.


and )
in extension
is Store
that mask
is from
db if the
phone
 
Back
Top