473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1656
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Steve Holden | last post by:
I was messing about with formatting and realized that the right kind of object could quite easily tell me exactly what accesses are made to the mapping in a string % mapping operation. This is a...
3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
7
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34...
4
by: Robert Manookian | last post by:
How do you format strings? i.e. In VB6: Format("AB34567", "@@@@@-@@") = "AB345-67" In .Net: ????????
2
by: David Veeneman | last post by:
How does one format a date column in a GridView control? I had assumed that the DataFormat string would do it, but MSDN only shows numeric formatting codes. Can dates be formatted using that...
4
by: Peter Newman | last post by:
the data input app im writing has some 30 + input fields and i want to be able to format them. I know i can use the .validate on each textbox and format the 'string' however this require loads...
11
by: Dustan | last post by:
Is there any builtin function or module with a function similar to my made-up, not-written deformat function as follows? I can't imagine it would be too easy to write, but possible... 'I am...
9
by: john coltrane | last post by:
Is there way to create a formatted string in a similar that is similar to sprintf? The same for printing, printf? C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical,...
6
by: Tomasz J | last post by:
Hello developers, I bind my TextBox control specyfying a format stored in my application global ApplicationContext object - it has a static string CurrencyFormat property. The problem - this...
2
by: Jean-Paul Calderone | last post by:
On Fri, 5 Sep 2008 14:24:16 -0500, Robert Dailey <rcdailey@gmail.comwrote: mystring = ( "This is a very long string that " "spans multiple lines and does " "not include line breaks or tabs "...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.