swade1987
swade1987

Reputation: 1673

String formatting

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

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

Upvotes: 0

Views: 314

Answers (3)

Nikola Radosavljević
Nikola Radosavljević

Reputation: 6911

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

Upvotes: 0

Firo
Firo

Reputation: 30803

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

Upvotes: 1

Dennis Traub
Dennis Traub

Reputation: 51624

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

Upvotes: 0

Related Questions