String Format Custom

  • Thread starter Thread starter Armando Rocha
  • Start date Start date
A

Armando Rocha

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
 
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();
}
 
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.
 
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.
 
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
 
Thanks for your post, but i solved the problem with RegularExpressions, see

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
 
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
 
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 said:
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
 
Back
Top