• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Dreama
只想要简简单单的快乐!
博客园    首页    新随笔    联系   管理     
[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

 

posted on 2017-12-21 09:46  Dreamma  阅读(5170)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3

© 本文章版权归 Dreama 所有, 转载授权请联系: cnxy@88.com

如果本文对您有帮助,欢迎支持原创

支付宝

支付宝扫码支持

微信

微信赞赏支持