How to mask string?

后端 未结 5 785
温柔的废话
温柔的废话 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:30

    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 assuming it's deliberate that you've missed out two of the 0s? It's not clear which 0s, admittedly...)

    Obviously you should validate that the string is the right length first...

提交回复
热议问题