How do I create my own custom ToString() format?
I would like to specify the format of the ToString
format, but I am not sure of the best way to handle this.
For example if I have the following specifiers
- EE = equipment
- ED = equipment description
- EI = equipment ID
so that if I used the ToString
as such:
eqp.ToString("EE-EI (ED)")
the output might be:
"CAT994-61 (Front end loader)"
Would the best way be to search for the substrings and do a token replacement? Does any one have an example of doing this?
I'm currently doing sequential string.Replace
, which works nicely.
public class Equipment
{
// (other class code)
public string ToString(string format)
{
string output = format;
output = output.Replace("EE", _EquipID);
output = output.Repla开发者_如何学运维ce("ED", _EquipDescription);
output = output.Replace("DI", _DepartID);
return output;
}
public override string ToString()
{
return _EquipID;
}
}
Combining string format, and overloading the ToString
you can do this:
public override string ToString(string myFormat)
{
myFormat = myFormat.Replace("EE", "{0}");
myFormat = myFormat.Replace("EI", "{1}");
myFormat = myFormat.Replace("ED", "{2}");
return String.Format(myFormat, this.equipment, this.description, this.id);
}
You can also use regular expressions to make the Replace
functions nicer.
This can be used as follows:
oProd.ToString("EE,ED,EI");
oProd.ToString("EE-ED (EI)"); // change order
oProd.ToString("ED-EE,EE,EE (EI)"); // multiple times
oProd.ToString("ED:EI"); // have some missing
etc
The String.Format gives you the flexibility to position the format variables any way like, have them multiple times, or drop some.
First override ToString()
Then if you want it to be dynamic you would have to parse the format string a token at a time and add the appropriate string to your output string. Using single letter format specifiers would make it a little simpler since you could parse it a character at a time. If it is a special character you output the appropriate data else output the character.
So something like this
public override string ToString(string format)
{
StringBuilder s = new StringBuilder();
foreach (char c in format)
{
switch (c)
{
case 'E':
s.Append(EquipID);
break;
case 'D':
s.Append(EquipDesc);
break;
case 'I':
s.Append(DepartID);
break;
default:
s.Append(c);
break;
}
}
return s.ToString();
}
This has the relatively minor advantage of doing it in one pass which is more efficient than multiple string replace calls.
String.Format("{0:foo}", object);
Will end up passing "foo"
to object.ToString(string format);
so it makes sense to design the arguments that your ToString(string format)
method takes with that in mind. In your example
String.Format("{0:EI - EE}", object);
would result in "EI - EE" being passed to the ToString method on object, so this could work as you describe, but it might make more sense to restrict the format to one representation at a time, like this
String.Format("{0:EI} - {0:EE}", object);
would be the way to get "CAT994-61" as output, and your ToString() method could be simpler
public override string ToString(string myFormat)
{
string str = null;
switch (myformat.ToLower())
{
case "ee": str = this.equipment; break;
case "ei": str = this.description; break;
case "ed": str = this.id; break;
default: str = this.ToString(); break;
}
return str;
}
If you reduce your format specifiers to single letters, that would be more consistent with the way other objects handle ToString formats.
Use String.Format
.
String.Format("{0}-{1} ({2})", "CAT994", 61, "Front end loader");
produces the expected output.
The format sounds like a good solution.
Perhaps a (few) nice regular expression(s) to cut your input variable into groups.
I would try to use as many existing formats if you can:
http://blog.stevex.net/string-formatting-in-csharp/
You just need to overload the ToString function in your object like
public override string ToString()
{
/* some code handling here */
return ""; //your own formated string
}
精彩评论