473,326 Members | 2,732 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,326 software developers and data experts.

String Format Custom

Hi,

Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in form
like this "25DD-68ED-EB8D-5E11", i try
String.Format("{0:####-####-####-####}", mystr), but not work, i think that
it is because the "mystr" it is already a string.

Anyone can help me?

Sorry my english....

--
Armando Rocha
(Portugal)
Mobile Developer

http://www.ifthensoftware.com

Jun 27 '08 #1
8 2186
In this case, I suspect the easiest option is to hand-crank it a
bit... something like below:

Marc

static void Main(string[] args)
{
// tests of various length...
Console.WriteLine(Format("25DD68EDEB8D5E112A"));
Console.WriteLine(Format("25DD68EDEB8D5E112"));
Console.WriteLine(Format("25DD68EDEB8D5E11"));
Console.WriteLine(Format("25DD68EDEB8D5E1"));
Console.WriteLine(Format("25DD68EDEB8D5E"));
Console.WriteLine(Format(""));
Console.WriteLine(Format("2"));
Console.WriteLine(Format("25"));
Console.WriteLine(Format("25D"));
Console.WriteLine(Format("25DD"));
Console.WriteLine(Format("25DDA"));
}
static string Format(string value)
{
const int BLOCK_SIZE = 4;
if (value == null) return null; // GIGO
StringBuilder sb = new StringBuilder(value);
for (int offset = BLOCK_SIZE; offset < sb.Length; offset
+= BLOCK_SIZE + 1)
{
sb.Insert(offset, '-');
}
return sb.ToString();
}
Jun 27 '08 #2
If you look at the ToString() overloads for a string you will see the
following:

public override string ToString();
public string ToString(IFormatProvider provider);
If you look at the ToString() overloads for an int for example, you will see
the following:

public override string ToString();
public string ToString(IFormatProvider provider);
public string ToString(string format);
public string ToString(string format, IFormatProvider provider);

If you notice, the int object has a ToString() overload that takes a format
as an argument "ToString(string format)" but you wont find that in any of
the ToString() overloads for a string.

My guess is that the "String.Format()" function internally tries to calls a
"ToString(string format)" method of the object you want to format but since
the String object does not have a matching overload it silently fails and no
formatting happens to the string.

I think that is the real reason the string is not being formatted... that
would be my best guess.


"Armando Rocha" <ar**********@ifthensoftware.comwrote in message
news:C8**********************************@microsof t.com...
Hi,

Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in
form like this "25DD-68ED-EB8D-5E11", i try
String.Format("{0:####-####-####-####}", mystr), but not work, i think
that it is because the "mystr" it is already a string.

Anyone can help me?

Sorry my english....

--
Armando Rocha
(Portugal)
Mobile Developer

http://www.ifthensoftware.com

Jun 27 '08 #3
Hi Marc,
Thanks for your post, but i solved the problem with RegularExpressions, see
below:

Regex re = new
Regex("([A-Fa-f0-9]{4})([A-Fa-f0-9]{4})([A-Fa-f0-9]{4})([A-Fa-f0-9]{4})");
string strAux = re.Replace("25DD68EDEB8D5E112A", "$1-$2-$3-$4");

it works fine to me.

--
Armando Rocha
Mobile Developer

http://www.ifthensoftware.com
"Marc Gravell" <ma**********@gmail.comescreveu na mensagem
news:a4**********************************@2g2000hs n.googlegroups.com...
In this case, I suspect the easiest option is to hand-crank it a
bit... something like below:

Marc

static void Main(string[] args)
{
// tests of various length...
Console.WriteLine(Format("25DD68EDEB8D5E112A"));
Console.WriteLine(Format("25DD68EDEB8D5E112"));
Console.WriteLine(Format("25DD68EDEB8D5E11"));
Console.WriteLine(Format("25DD68EDEB8D5E1"));
Console.WriteLine(Format("25DD68EDEB8D5E"));
Console.WriteLine(Format(""));
Console.WriteLine(Format("2"));
Console.WriteLine(Format("25"));
Console.WriteLine(Format("25D"));
Console.WriteLine(Format("25DD"));
Console.WriteLine(Format("25DDA"));
}
static string Format(string value)
{
const int BLOCK_SIZE = 4;
if (value == null) return null; // GIGO
StringBuilder sb = new StringBuilder(value);
for (int offset = BLOCK_SIZE; offset < sb.Length; offset
+= BLOCK_SIZE + 1)
{
sb.Insert(offset, '-');
}
return sb.ToString();
}
Jun 27 '08 #4
On Apr 29, 6:54*am, "Armando Rocha" <armandoro...@ifthensoftware.com>
wrote:
Hi,

Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in form
like this "25DD-68ED-EB8D-5E11", i try
String.Format("{0:####-####-####-####}", mystr), but not work, i think that
it is because the "mystr" it is already a string.

Anyone can help me?

Sorry my english....

--
Armando Rocha
(Portugal)
Mobile Developer

http://www.ifthensoftware.com
Hi,

You will have to split your string and then use String.Format
Jun 27 '08 #5
Thanks for your post, but i solved the problem with RegularExpressions, see
below:
Fair enough; whatever works and is easy for you to maintain.

For the record, the regex will be slower, but it is unlikely that this
is significant (unless you are doing some serious data processing);
but some metrics from a performance test rig:

StringBuilder: 234ms; chk=9500000
Regex (compiled): 1580ms; chk=9500000
Regex (new): 21848ms; chk=9500000

(500k iterations, release mode, console, no debugger, etc) - so the
regex via "new" each time is 100 times slower. If you want to use
regex, I recommend pre-compiling it so that it is only 7 times slower:

const string PATTERN = @"([A-Fa-f0-9]{4})([A-Fa-f0-9]{4})([A-Fa-
f0-9]{4})([A-Fa-f0-9]{4})";
static readonly Regex regex = new Regex(PATTERN,
RegexOptions.Compiled);

static string Format2(string value)
{
return regex.Replace(value, "$1-$2-$3-$4");
}

Marc
Jun 27 '08 #6
Actually - an accidental white lie; I made a small optimisation to the
original code for the perf test:

int maxLen = ((value.Length / BLOCK_SIZE) + 1) * BLOCK_SIZE;
StringBuilder sb = new StringBuilder(value, maxLen);

i.e. start the StringBuilder with enough space for the extra chars, so
it doesn't have to reallocate and blit the buffer.

(without this is was 309ms)

;-p
Jun 27 '08 #7
Ok Marc,

You convinced me to use it the StringBuilder :)...

private string FormatKey(String value)
{
const Int32 BLOCK_SIZE = 4;
Int32 maxLen = ((value.Length / BLOCK_SIZE) + 1) * BLOCK_SIZE;
StringBuilder sb = new StringBuilder(value, maxLen);
for (Int32 offset = BLOCK_SIZE; offset < sb.Length; offset +=
BLOCK_SIZE + 1)
{
sb.Insert(offset, '-');
}
return sb.ToString();
}

Thanks.

--
Armando Rocha
Mobile Developer

http://www.ifthensoftware.com
"Marc Gravell" <ma**********@gmail.comescreveu na mensagem
news:33**********************************@c65g2000 hsa.googlegroups.com...
Actually - an accidental white lie; I made a small optimisation to the
original code for the perf test:

int maxLen = ((value.Length / BLOCK_SIZE) + 1) * BLOCK_SIZE;
StringBuilder sb = new StringBuilder(value, maxLen);

i.e. start the StringBuilder with enough space for the extra chars, so
it doesn't have to reallocate and blit the buffer.

(without this is was 309ms)

;-p
Jun 27 '08 #8
Armando Rocha wrote:
Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in
form like this "25DD-68ED-EB8D-5E11", i try
String.Format("{0:####-####-####-####}", mystr), but not work, i think
that it is because the "mystr" it is already a string.
Have you considered the super simple solution a
format string like
"{0}{1}{2}{3}-{4}{5}{6}{7}-{8}{9}{10}{11}-{12}{13}{14}{15}" ?

It is not elegant, but the resulting code is very readable.

Arne
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sterling Ledet | last post by:
I am trying to create a web service that takes a string from my web server in the following format: Mon, 6 Oct 2003 18:39:47 UTC and put's in a datetime so it can then be reformatted in C# as...
3
by: Mark | last post by:
I have a Decimal or a Double - your choice. I'd like to print the number as string with a specified number of decimal places. Let's say my number 110.5, and I'd like to capture it in a string...
3
by: Shimon Sim | last post by:
I have control. One of the properties is implemented as StringCollection. I didn't use any Editor attribute for it. During Design time system shows dialog similar to Items in DropDownList but Add...
4
by: Chris Dunaway | last post by:
I have a table in the database with a phone number field. The phone number is stored without any punctuation (e. g. 9995551234). I wish to take that string and format it for display (e. g. (999)...
5
by: Burak | last post by:
Hello, I would like to format the string "11304200" into "11-3042.00". Can I do this with String.Format method? I have not come across any good documentation. Thank you,
50
by: z. f. | last post by:
HI, i have string in format dd/mm/yyyyy hh:mm:ss and giving this as an input to DateTime.Parse gives a string was not recognized as a valid date time format string error. how do i make the parse...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
3
by: gregcm | last post by:
I've got a custom control that is trying to allow the user to enter a DateTime in a definable format and I was wondering if there was an easy way to alter the look of said control based on the...
7
by: Andrus | last post by:
How to create format string for decimal data type which shows blank for zero and default format otherwize ? I tried format string "f;f;#" but this shows f for nonzero numbers. Andrus. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.