[C#]使用IFormattable接口來實現字符串格式化



本文為原創文章、源代碼為原創代碼,如轉載/復制,請在網頁/代碼處明顯位置標明原文名稱、作者及網址,謝謝!


開發工具:VS2017

語言:C#

DotNet版本:.Net FrameWork 4.0及以上

一、編寫一個Person類,代碼如下:

    class Person
    {
        public string FirstName { set; get; }
        public string LastName { set; get; }
    }

並讓Person類繼承IFormattable,代碼如下:

    class Person:IFormattable
    {
        public string FirstName { set; get; }
        public string LastName { set; get; }

        public string ToString(string format, IFormatProvider formatProvider)
        {
           //關鍵代碼,后面給出
        }
    }

這里將會列出需要實現IFormattable的方法ToString(string format, IFormatProvider formatProvider),這里是關鍵代碼,用來格式字符串,暫時不給出,由后面給出。

二、編寫 PersonFormatter類,讓其繼承IFormatProvider及ICustomFormatter,用於對字符串進行格式化,代碼如下:

    class PersonFormatter : IFormatProvider,ICustomFormatter
    {
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            //Format實現代碼
        }

        public object GetFormat(Type formatType)
        {
            //GetFormat實現代碼
        }
    }

Format:用於格式化字符串

Format的實現代碼如下:

        Person person = arg as Person;
        switch(format)
        {
            case "CH":return $"{person.LastName} {person.FirstName}";
            case "EN":return $"{person.FirstName} {person.LastName}";
            default: return $"{person.LastName} {person.FirstName}";
        }

GetFormat的實現代碼如下:

        if (formatType == typeof(ICustomFormatter)) return this;
        return null;

因此,PersonFormatter類的代碼如下:

    class PersonFormatter : IFormatProvider,ICustomFormatter
    {
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            Person person = arg as Person;
            switch(format)
            {
                case "CH":return $"{person.LastName} {person.FirstName}";
                case "EN":return $"{person.FirstName} {person.LastName}";
                default: return $"{person.LastName} {person.FirstName}";
            }
        }

        public object GetFormat(Type formatType)
        {
            if (formatType == typeof(ICustomFormatter)) return this;
            return null;
        }
    }

三、實現Person類IFormattable接口ToString方法,代碼如下:

        ICustomFormatter customFormatter = formatProvider as ICustomFormatter;
        if (customFormatter == null) return this.ToString();
        return customFormatter.Format(format, this, null);

最終Person類代碼如下:

    class Person:IFormattable
    {
        public string FirstName { set; get; }
        public string LastName { set; get; }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            ICustomFormatter customFormatter = formatProvider as ICustomFormatter;
            if (customFormatter == null) return this.ToString();
            return customFormatter.Format(format, this, null);
        }
    }

四、使用Peson類的ToString方法,編寫以下代碼:

        Person p1 = new Person { FirstName = "XY", LastName = "CN" };
        PersonFormatter pf = new PersonFormatter();
        string s1 = p1.ToString("CN", pf);
        Console.WriteLine(s1);
        string s2 = p1.ToString("EN", pf);
        Console.WriteLine(s2);

五、運行結果:

六、附上完整源碼:

    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person { FirstName = "XY", LastName = "CN" };
            PersonFormatter pf = new PersonFormatter();
            string s1 = p1.ToString("CN", pf);
            Console.WriteLine(s1);
            string s2 = p1.ToString("EN", pf);
            Console.WriteLine(s2);
        }
    }

    class PersonFormatter : IFormatProvider,ICustomFormatter
    {
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            Person person = arg as Person;
            switch(format)
            {
                case "CH":return $"{person.LastName} {person.FirstName}";
                case "EN":return $"{person.FirstName} {person.LastName}";
                default: return $"{person.LastName} {person.FirstName}";
            }
        }

        public object GetFormat(Type formatType)
        {
            if (formatType == typeof(ICustomFormatter)) return this;
            return null;
        }
    }

    class Person:IFormattable
    {
        public string FirstName { set; get; }
        public string LastName { set; get; }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            ICustomFormatter customFormatter = formatProvider as ICustomFormatter;
            if (customFormatter == null) return this.ToString();
            return customFormatter.Format(format, this, null);
        }
    }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM