字符串格式

时间:2012-01-06 10:05:12

标签: c# string string-formatting

我可以编写一个应用程序来转换长度为8到12个字符的字符串(由数字组成)(参见下面的示例)

  • 1404336133
  • 4174728823
  • 0587035281

基本上我想将上面的字符串转换为特定的格式(存储在配置文件中),暂时如下所示。

<add key="Consumer_Code_Format_For_Code_Length_Eight" value="#### ####"/>
<add key="Consumer_Code_Format_For_Code_Length_Nine" value="### ### ###"/>
<add key="Consumer_Code_Format_For_Code_Length_Ten" value="### #### ###"/>
<add key="Consumer_Code_Format_For_Code_Length_Eleven" value="#### ### ###"/>
<add key="Consumer_Code_Format_For_Code_Length_Twelve" value="#### #### ####"/>

我目前正在使用以下代码格式化代码......

public static string FormatCode(string code)
{
    switch (code.Length.ToString())
    {
        case "8":
            string codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Eight"];
            code = String.Format("{0:" + codeFormat + "}", Double.Parse(code));
            break;

        case "9":
            codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Nine"];
            code = String.Format("{0:" + codeFormat + "}", Double.Parse(code));
            break;

        case "10":
            codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Ten"];
            code = String.Format("{0:" + codeFormat + "}", Double.Parse(code));
            break;

        case "11":
            codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Eleven"];
            code = String.Format("{0:" + codeFormat + "}", Double.Parse(code));
            break;

        case "12":
            codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Twelve"];
            code = String.Format("{0:" + codeFormat + "}", Double.Parse(code));
            break;

        default:
            codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Eight"];
            code = String.Format("{0:" + codeFormat + "}", Double.Parse(code));
            break;
    }

    // Finally return the newly formatted code
    return code;
}

但是,对于代码0587035281,它显示“58 7035 281”,因此删除了我需要的前导零。

任何想法如何阻止这一点,我的代码是否有任何错误或可疑?

期待您的回复

3 个答案:

答案 0 :(得分:1)

public static string FormatCode(string code)
{
    switch (code.Length.ToString())
    {
        case "8":
            string codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Eight"];
            break;

        case "9":
            codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Nine"];
            break;

        case "10":
            codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Ten"];
            break;

        case "11":
            codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Eleven"];
            break;

        case "12":
            codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Twelve"];
            break;

        default:
            codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Eight"];
            break;
    }

    char[] result = new char[codeformat.Length];

    int used = 0;
    for(i = 0; i < codeformat.Length; i++)
    {
        if (codeformat[i] == '#')
        {
            result[i] = code[used];
            used++;
        }
        else
            result[i] = codeformat[i];
    }
    // Finally return the newly formatted code
    return new string(result);
}

答案 1 :(得分:0)

Double值实际上存储为数字,因此在使用Double.Parse()时会自动删除前导零。

再次添加nleading零,你可以做类似的事情(没有测试,从头顶写。)记住原始长度,然后比较结果并根据需要添加尽可能多的前导零。 / p>

public static string FormatCode(string code)
{
    int originalLength = code.Length;

    switch (originalLength)
    {
        case 8:
            string codeFormat = ...;
            code = ...;
            break;
        case 9:
            codeFormat = ...;
            code = ...;
            break;
        // ...

    while (code.Length < originalLength) {
        code = "0" + code;
    }

    return code;
}

答案 2 :(得分:0)

没有可以内联的漂亮解决方案,你必须自己进行处理。因为这不是你正在处理的数字,也许你可能需要在那里有一些非数字文字,最好只是每N个字符插入一个空格。这将是这样的:

int chunkSize = 3;
string a = "0123456789";
int len = a.Length;
StringBuilder sb = new StringBuilder(len + len / chunkSize);
int firstIdx = len % chunkSize;
sb.Append(a.Substring(0, firstIdx));
sb.Append(" ");
for (int i = firstIdx; i < len; i += chunkSize)
{
    sb.Append(a.Substring(i, chunkSize));
    sb.Append(" ");
}
a = sb.ToString();