469,632 Members | 1,732 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

formatting strings

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
Nov 17 '05 #1
3 1509
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" <Pe*****@dslextreme.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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


Nov 17 '05 #2

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]" <x-****@no-spam-please.hotpop.com> wrote
in message news:ux**************@TK2MSFTNGP09.phx.gbl...
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" <Pe*****@dslextreme.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #3
"Perecli Manole" <Pe*****@dslextreme.com> wrote in message
news:#f**************@tk2msftngp13.phx.gbl...
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[i] == 'x')
template[i] = (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);

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Steve Holden | last post: by
7 posts views Thread by BBFrost | last post: by
4 posts views Thread by Robert Manookian | last post: by
2 posts views Thread by David Veeneman | last post: by
4 posts views Thread by Peter Newman | last post: by
11 posts views Thread by Dustan | last post: by
9 posts views Thread by john coltrane | last post: by
6 posts views Thread by Tomasz J | last post: by
2 posts views Thread by Jean-Paul Calderone | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.