简体   繁体   中英

String formatting

I can written an application which converts strings (made of numbers) from 8 - 12 characters in length (see examples below)

  • 1404336133
  • 4174728823
  • 0587035281

Basically I want to convert the strings above into a specific format (stored in a config file) for the time being its as shown below.

<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="#### #### ####"/>

I am currently using the following code to format the codes ...

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;
}

However, for the code 0587035281 it displays "58 7035 281" therefore removing the leading zero which I require.

Any ideas how to stop this and also is there anything wrong or suspicious with my code?

Looking forward to your reply

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

Double values are actually stored as numbers, so leading zeroes are being automatically removed when using Double.Parse() .

Tho add the nleading zeroes once again, you can do something similar to this (not tested, written from the top of my head.) Remember the original length, then compare the result and add as many leading zeroes as required.

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;
}

There's no pretty solution you can do inline, you'll have to do your own processing. Since this is not really a number you're dealing with, and maybe you may need to have some non-numeric literals there, best that you just insert a space every N characters. This would go something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM