473,561 Members | 3,803 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

formatting strings

I want to convert a string "12341234123412 34" 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 1666
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*****@dslext reme.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
I want to convert a string "12341234123412 34" 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.c om> wrote
in message news:ux******** ******@TK2MSFTN GP09.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*****@dslext reme.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
I want to convert a string "12341234123412 34" 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*****@dslext reme.com> wrote in message
news:#f******** ******@tk2msftn gp13.phx.gbl...
I want to convert a string "12341234123412 34" 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(ICustomF ormatter)
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 ICustomFormatte r 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 , ICustomFormatte r
{
public static readonly CustomFormatter Instance = new CustomFormatter ();

private CustomFormatter () {}

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

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

string s = arg.ToString();
char[] template = format.ToCharAr ray();
int j = 0;
for (int i=0; i<template.Leng th; 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 = "12341234123412 34";
string b = String.Format(C ustomFormatter. 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
1629
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 fairly well-known technique, modified to tell me what keys would need to be present in any mapping used with the format. class Everything: def...
3
5921
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 terminated by pressing ++ and then terminate the process. I searched the entire internet and found out that there could be two things wrong (both...
7
3920
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 123.0000 123 i.e. I want to trim trailing zeros and (decimal point if no decimal values
4
4020
by: Robert Manookian | last post by:
How do you format strings? i.e. In VB6: Format("AB34567", "@@@@@-@@") = "AB345-67" In .Net: ????????
2
2258
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 property, or is it done some other way? Thanks in advance. -- David Veeneman Foresight Systems
4
2922
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 of repetitive codeing. Is there a way possible to globally format the textboxes both on the .validate function, as well as when the enter key is...
11
4985
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 coding, and he coded last week.' ('coding', 'coded', 'week') expanded (for better visual): ('coding', 'coded', 'week')
9
2311
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, and hex but these do not seem to allow for specifying the number of decimals, left/right placement, or string formatting. Thanks
6
4579
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 works fine: Text='<%# Eval("Price", ApplicationContext.CurrencyFormat) %>'
2
1594
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 " "from the source file between " "the strings partitions.") Jean-Paul
0
7647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7859
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8088
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7930
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5472
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5187
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3617
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3600
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.