自定义格式化字符串-创新互联
----------------------------------------------主程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { //自定义格式化字符串 Voctor v = new Voctor(3d, 4d, 5d); Console.WriteLine("{0:zhang}", v);//Zhang:(3,4,5) Console.WriteLine("{0:liu}", v);//Liu:(3,4,5) Console.ReadKey(); } } }
----------------------------------------------Voctor.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { //自定义格式化字符串类或结构必须实现IFormattable接口 public class Voctor : IFormattable { double x, y, z; public Voctor(double d1, double d2, double d3) { this.x = d1; this.y = d2; this.z = d3; } //format表示传递的说明符,例如N(不用考虑formatProvider) public string ToString(string format, IFormatProvider formatProvider) { if (format == null) { return ToString(); } //不区分大小写 string formatUpper = format.ToUpper(); switch (formatUpper) { case "ZHANG": return "Zhang:" + ToString(); case "LIU": return "Liu:" + ToString(); default: return ToString(); } } public override string ToString() { return string.Format("({0},{1},{2})", x, y, z); } } }
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页题目:自定义格式化字符串-创新互联
文章位置:http://ybzwz.com/article/gdhjo.html