4
I have the following string "49988070405"
and I want to format it for "(49) 98807-0405"
.
I tried the code:
Convert.ToUInt64("49988070405").ToString(@"\(00\)\ 00000\-0000");
But to no avail
4
I have the following string "49988070405"
and I want to format it for "(49) 98807-0405"
.
I tried the code:
Convert.ToUInt64("49988070405").ToString(@"\(00\)\ 00000\-0000");
But to no avail
4
You can do it this way:
long.Parse("49988070405").ToString(@"(00) 00000-0000"); // (49) 98807-0405
Another example, using extensions:
using System;
public static class Program
{
public static void Main()
{
var phoneString = "49988070405";
Console.WriteLine(phoneString.FormatPhoneNumber()); // (49) 98807-0405
var phone = 49988070405;
Console.WriteLine(phone.FormatPhoneNumber()); // (49) 98807-0405
}
public static string FormatPhoneNumber(this long number) {
return number.ToString(@"(00) 00000-0000");
}
public static string FormatPhoneNumber(this string number) {
return long.Parse(number).FormatPhoneNumber();
}
}
0
You can do the following, follow an example:
String.Format("{0:(###) ###-####}", 8005551212);
Which has as a result:
(800) 555-1212
But I want the result (49) 98807-0405, I tried that example there, it didn’t work.
0
You can use the following:
string phone = "49988070405"
string formatted = string.Format("{0:(##) #####-####}", phone);
Browser other questions tagged c#
You are not signed in. Login or sign up in order to post.
The first way was enough, but it is strange that there is no more direct way to do it. First I had to convert the string for long, then to string again to be able to format.
– Matheus Saraiva
@Matheussaraiva Because this is the right one, what you want to convert is a number, and C# is a strongly typed language, so it makes no sense to use a string to store a number. Is there any reason why the number really needs to be a string? 'Cause the right thing would be for you to already have it as long.
– Vinícius Lima