• [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
  • 相关阅读:
    npm install 报错 npm ERR! path /Users/lijuanwang/work/kr/krweb/node_modules/nodesass
    elementui tree权限树
    new Map()详解
    字符串中判断是否包含某个字符
    vue3 vuex4 store的响应式取值
    腾讯WeMap微信小程序报错之getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json
    vue 加密解密 ts项目中
    Object.entries()详解
    数组对象中,匹配value,删除对象
    js拼接html传参数
  • 原文地址:https://www.cnblogs.com/cncc/p/8078599.html
Copyright © 2020-2023  润新知