How to mask string?

后端 未结 5 721
温柔的废话
温柔的废话 2021-01-18 02:51

I have a string with value \"1131200001103\".

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

Thanks

5条回答
  •  伪装坚强ぢ
    2021-01-18 03:23

    You can try a regular expression and put this inside an extension method ToMaskedString()

    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());
    

提交回复
热议问题