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);
CodePudding user response:
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)}";
CodePudding user response:
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.