formatting strings

Discussion in 'ASP .Net' started by Perecli Manole, Oct 23, 2003.

  1. I want to convert a string "1234123412341234" to "1234-1234-1234-1234".
    This does not work:
    String.Format("{0:xxxx-xxxx-xxxx-xxxx}",s)
    What will? Although I know there are many other solutions, I am only
    interested in ones that use the new braket {0:} style formating because many
    web controls require this.

    Thanks
    Perry
    Perecli Manole, Oct 23, 2003
    #1
    1. Advertising

  2. Hi,

    You can try {0:####-####-####-####} as 'x' is not a reserved placeholder
    character.

    You can also refer to the "Custom Numeric Format Strings" topic in the MSDN
    Library.

    --
    Dmitriy Lapshin [C# / .NET MVP]
    X-Unity Test Studio
    http://x-unity.miik.com.ua/teststudio.aspx
    Bring the power of unit testing to VS .NET IDE

    "Perecli Manole" <> wrote in message
    news:%...
    > I want to convert a string "1234123412341234" to "1234-1234-1234-1234".
    > This does not work:
    > String.Format("{0:xxxx-xxxx-xxxx-xxxx}",s)
    > What will? Although I know there are many other solutions, I am only
    > interested in ones that use the new braket {0:} style formating because

    many
    > web controls require this.
    >
    > Thanks
    > Perry
    >
    >
    Dmitriy Lapshin [C# / .NET MVP], Oct 23, 2003
    #2
    1. Advertising

  3. I tried this and it doesn't work, the reason being that my field is a string
    field and # reserved place holder is for numeric fields as you have stated
    yourself. I could not find any formatting examples for string types.

    Thanks
    Perry

    "Dmitriy Lapshin [C# / .NET MVP]" <> wrote
    in message news:...
    > Hi,
    >
    > You can try {0:####-####-####-####} as 'x' is not a reserved placeholder
    > character.
    >
    > You can also refer to the "Custom Numeric Format Strings" topic in the

    MSDN
    > Library.
    >
    > --
    > Dmitriy Lapshin [C# / .NET MVP]
    > X-Unity Test Studio
    > http://x-unity.miik.com.ua/teststudio.aspx
    > Bring the power of unit testing to VS .NET IDE
    >
    > "Perecli Manole" <> wrote in message
    > news:%...
    > > I want to convert a string "1234123412341234" to "1234-1234-1234-1234".
    > > This does not work:
    > > String.Format("{0:xxxx-xxxx-xxxx-xxxx}",s)
    > > What will? Although I know there are many other solutions, I am only
    > > interested in ones that use the new braket {0:} style formating because

    > many
    > > web controls require this.
    > >
    > > Thanks
    > > Perry
    > >
    > >

    >
    Perecli Manole, Oct 23, 2003
    #3
  4. "Perecli Manole" <> wrote in message
    news:#...
    > I want to convert a string "1234123412341234" to "1234-1234-1234-1234".
    > This does not work:
    > String.Format("{0:xxxx-xxxx-xxxx-xxxx}",s)
    > What will? Although I know there are many other solutions, I am only
    > interested in ones that use the new braket {0:} style formating because

    many
    > web controls require this.
    >


    You can supply a custom formatter to accomplish this. String.Format takes an
    optional IFormatProvider first parameter. It passes typeof(ICustomFormatter)
    to the GetFormat method of this object to see if the format provider
    provides a custom formatter. If so, it calls that custom formatter to let it
    handle the formatting of the argument.

    Here's an example of an IFormatProvider and an ICustomFormatter combined
    into one reusable class. It recognizes 'x' characters in the formatting
    string as placeholders for characters in the string argument.

    public sealed class CustomFormatter: IFormatProvider, ICustomFormatter
    {
    public static readonly CustomFormatter Instance = new CustomFormatter();

    private CustomFormatter() {}

    public object GetFormat(Type formatType)
    {
    return formatType == typeof(ICustomFormatter) ? this : null;
    }

    public string Format(string format, object arg, IFormatProvider
    formatProvider)
    {
    if (format == null)
    return String.Format(formatProvider, "{0}", arg);

    string s = arg.ToString();
    char[] template = format.ToCharArray();
    int j = 0;
    for (int i=0; i<template.Length; i++)
    if (template == 'x')
    template = (j >= s.Length ? ' ' : s[j++]);

    return new string(template);
    }
    }

    To use this custom formatter in your example, you would do this:

    string a = "1234123412341234";
    string b = String.Format(CustomFormatter.Instance,
    "{0:xxxx-xxxx-xxxx-xxxx}", a);
    Bret Mulvey [MS], Oct 24, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guadala Harry

    Formatting Strings

    Guadala Harry, Oct 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    381
    Guadala Harry
    Oct 28, 2004
  2. Joona I Palaste

    Question on formatting Strings

    Joona I Palaste, Oct 19, 2003, in forum: Java
    Replies:
    3
    Views:
    386
    Paul Lutus
    Oct 20, 2003
  3. Rick

    formatting strings

    Rick, Oct 16, 2003, in forum: C Programming
    Replies:
    10
    Views:
    609
    Irrwahn Grausewitz
    Oct 17, 2003
  4. Steve Holden

    Enumerating formatting strings

    Steve Holden, Apr 18, 2005, in forum: Python
    Replies:
    14
    Views:
    507
    Michael Spencer
    Apr 21, 2005
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    741
    Malcolm
    Jun 24, 2006
Loading...

Share This Page