将零填充到字符串的开头

0 c#

我写了一个有趣的代码,如果字符串的第一个字符是空白的,它将用"0"替换那个空格,它可以工作,但就像我说它很有趣.有没有更好的方式写同样的东西?

    char ch = value[0];
    if (ch == ' ')
    {
        value = value.Trim();
        value = "0" + value;
    }
    return value;
Run Code Online (Sandbox Code Playgroud)

Mat*_*son 8

它很简单:

if (value.Length > 0 && value[0] == ' ')
    value = '0' + value.Substring(1);
Run Code Online (Sandbox Code Playgroud)