-1

I have string value like below example for fax

string fax="1111111111";

I need below result for above string to add special character for fax format like below.

(111)-111-1111

my code for reference because my question going down please help any to get result

        var list = (dynamic)null;
        if (!String.IsNullOrEmpty(faxdos.medicalRecordsFax) && !String.IsNullOrEmpty(faxdos.fax))
        {

            list = new List<SelectListItem>
        {
          new SelectListItem{ Text=String.Format("{0:(###)-###-####}", faxdos.medicalRecordsFax)+" - Medical Records Fax", Value = faxdos.medicalRecordsFax},
          new SelectListItem{ Text=String.Format("{0:(###)-###-####}", faxdos.fax), Value = faxdos.fax },
        };
        }
        else if (!String.IsNullOrEmpty(faxdos.medicalRecordsFax))
        {
            list = new List<SelectListItem>
        {
          new SelectListItem{ Text=String.Format("{0:(###)-###-####}", faxdos.medicalRecordsFax)+" - Medical Records Fax", Value = faxdos.medicalRecordsFax},

        };
        }
        else if (!String.IsNullOrEmpty(faxdos.fax))
        {
            list = new List<SelectListItem>
        {
          new SelectListItem{ Text=String.Format("{0:(###)-###-####}", faxdos.fax), Value = faxdos.fax },
        };
        }
        else
        {
            list = new List<SelectListItem>
        {
          new SelectListItem{ Text="", Value = "" },
        };
        }
        // ViewBag.emp = list;
        var result = new SelectList(list, "Text", "Value");
        

        return Json(result, JsonRequestBehavior.AllowGet);
coder rock
  • 113
  • 6
  • Generally, in North America, you'd format that as `(111) 111-1111`, i.e., using a space instead of the first dash – Flydog57 Feb 04 '22 at 19:04
  • my client have requirement to display like this in the dropdown list. @Jesse – coder rock Feb 04 '22 at 19:10
  • That doesn't answer the question. You need to include code that shows the effort you've already put in to find a solution yourself. If you don't put in effort, nobody else is going to want to. – Jesse Feb 04 '22 at 19:10
  • my client asked like this format. @Flydog57 – coder rock Feb 04 '22 at 19:11
  • shall i have to post whole code then you will get i need this requirements. @Jesse – coder rock Feb 04 '22 at 19:12
  • Include a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Read the link I posted as well. It says what is needed. – Jesse Feb 04 '22 at 19:13
  • I have updated my question and can you please post your answer now if you know. @Jesse – coder rock Feb 04 '22 at 19:16
  • I think now you will add answer. @Jesse – coder rock Feb 04 '22 at 19:18
  • The code you posted still doesn't show an attempt to answer your own question, you're just shoving the phone number into a `SelectListItem`, and this is definitely not minimal. Please read the links I sent. – Jesse Feb 04 '22 at 19:18
  • It's actually already been answered [here](https://stackoverflow.com/questions/188510/how-to-format-a-string-as-a-telephone-number-in-c-sharp) – Jesse Feb 04 '22 at 19:25
  • you kind information that is not working for me. @Jesse – coder rock Feb 04 '22 at 19:39
  • i have updated the question above. @Jesse – coder rock Feb 04 '22 at 19:42

2 Answers2

2

well how about just writing code to do it

string fax="1111111111";

string str2 = $"({fax.Substring(0,3)})-{fax.SubString(3,3)}-{fax.Substring(6,4)}";
pm100
  • 48,078
  • 23
  • 82
  • 145
1

If you want to use the var result = string.Format("{0:(###)-###-####}", someValue) formatting mechanism, then the value you are formatting needs to be a number, not a string. So you could do something like this:

var telNoString = "1111111111";
if (long.TryParse(telNoString, out var telno))
{
    var result = string.Format("{0:(###)-###-####}", telno);
    Debug.WriteLine(result);
}

Which will result in (111)-111-1111 in the debug console.

Flydog57
  • 6,851
  • 2
  • 17
  • 18
  • @MazharKhan: The reason for your downvotes was that your original question was well below acceptable. Once you included some code, it was answerable – Flydog57 Feb 04 '22 at 20:19