溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

自定義格式化字符串

發(fā)布時間:2020-07-12 18:46:29 來源:網(wǎng)絡 閱讀:397 作者:1473348968 欄目:編程語言

----------------------------------------------主程序

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
{
    //自定義格式化字符串類或結(jié)構(gòu)必須實現(xiàn)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();
            }
            //不區(qū)分大小寫
            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);
        }
    }
}

 

自定義格式化字符串

自定義格式化字符串

 

 

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI