468,554 Members | 1,896 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 1967
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sterling Ledet | last post: by
3 posts views Thread by Shimon Sim | last post: by
4 posts views Thread by Chris Dunaway | last post: by
50 posts views Thread by z. f. | last post: by
8 posts views Thread by Lucky | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.