0

I have a string of telephone number. The format I wanted to use is like this

(044) 463-2256

How can I format my string like this?

string tel = "0444632256";
var match = Regex.Replace(tel, @"");

I tried using Regex.Replace but I don't know what the format that I will put in.

3 Answers3

0

If the Length of the string is always constant(10) , then wecan use the below method to format

string tel = "0444632256";
var format = $"({tel.Substring(0, 3)}) {tel.Substring(3, 3)}-{tel.Substring(6, 4)}";
Console.WriteLine(format);
Test12345
  • 1,625
  • 1
  • 12
  • 21
  • How can I remove the format text has been formated? –  Oct 04 '19 at 05:02
  • Can you elaborate more on this – Test12345 Oct 04 '19 at 05:04
  • I placed this code on my unfocused event. My focused event's function is to remove the format so that when I edit the string it always formatted when I unfocused –  Oct 04 '19 at 05:06
0

You can see below answer:

Convert.ToInt64(tel).ToString("###-###-#### ####")

How to format a string as a telephone number in C#

0

Try this code:

string tel = "0444632256";
tel=String.Format("{0:(###) ###-####}", Int64.Parse(tel));

Output:

(44) 463-2256

Tinh Linh
  • 46
  • 2