How do I hide a row?

I have a string with the value "1131200001103".

How can I display it as a string in this format "11-312-001103" using Response.Write (value)?

thank

+3


source to share


4 answers


This gives the desired result

string result = Int64.Parse(s.Remove(5,2)).ToString("00-000-000000");

      



assuming you want to dump the 2 characters at the position of the first two zeros.

+11


source


Any reason you don't want to just use Substring

?

string dashed = text.Substring(0, 2) + "-" +
                text.Substring(2, 3) + "-" +
                text.Substring(7);

      

Or:



string dashed = string.Format("{0}-{1}-{2}", text.Substring(0, 2),
                              text.Substring(2, 3), text.Substring(7));

      

(I'm guessing it's overlooked that you missed two of the 0s? Not clear which 0s are, admittedly ...)

Obviously, you have to check that the string is of the correct length first ...

+12


source


You can try regex and put it in ToMaskedString () extension method

public static class StringExtensions
{
    public static string ToMaskedString(this String value)
    {
        var pattern = "^(/d{2})(/d{3})(/d*)$";
        var regExp = new Regex(pattern);
        return regExp.Replace(value, "$1-$2-$3");
    }
}

      

Then call

respne.Write(value.ToMaskedString());

      

+2


source


Maybe something like

string result = str.SubString(0, 2) + "-" + str.SubString(2, 3) + "-" + str.SubString(7);

      

str is the string "11312000011103"

+2


source







All Articles