zoukankan      html  css  js  c++  java
  • 枚举格式化字符串

    使用Enum.ToString(string)方法创建新的字符串对象,以表示枚举成员的数值、十六进制值或字符串值。 此方法采用某个枚举格式化字符串指定希望返回的值。

    下表列出了枚举格式化字符串及其返回的值。 这些格式说明符不区分大小写。

     

    格式字符串

    结果

    G 或 g

    如有可能,将枚举项显示为字符串值,否则显示当前实例的整数值。 如果枚举定义中设置了 Flags 特性,则串联每个有效项的字符串值并将各值用逗号分开。 如果未设置 Flags 特性,则将无效值显示为数字项。 下面的示例阐释 G 格式说明符。

     
    Console.WriteLine(ConsoleColor.Red.ToString("G"));         // Displays Red
    FileAttributes attributes = FileAttributes.Hidden |
                                FileAttributes.Archive;
    Console.WriteLine(attributes.ToString("G"));   // Displays Hidden, Archive                               
    
    
    

    F 或 f

    如有可能,将枚举项显示为字符串值。 如果值可以完全显示为枚举项的总和(即使未提供 Flags 特性),则串联每个有效项的字符串值并将各值用逗号分开。 如果值不能完全由枚举项确定,则将值格式化为整数值。 下面的示例阐释 F 格式说明符。

     
    Console.WriteLine(ConsoleColor.Blue.ToString("F"));       // Displays Blue
    FileAttributes attributes = FileAttributes.Hidden | 
                                FileAttributes.Archive;
    Console.WriteLine(attributes.ToString("F"));   // Displays Hidden, Archive                               
    
    
    

    D 或 d

    以尽可能短的表示形式将枚举项显示为整数值。 下面的示例阐释 D 格式说明符。

     
    Console.WriteLine(ConsoleColor.Cyan.ToString("D"));         // Displays 11
    FileAttributes attributes = FileAttributes.Hidden |
                                FileAttributes.Archive;
    Console.WriteLine(attributes.ToString("D"));                // Displays 34                               
    
    
    

    X 或 x

    将枚举项显示为十六进制值。 按需要将值表示为带有前导零,以确保值的长度最少有八位。 下面的示例阐释 X 格式说明符。

     
    Console.WriteLine(ConsoleColor.Cyan.ToString("X"));   // Displays 0000000B
    FileAttributes attributes = FileAttributes.Hidden |
                                FileAttributes.Archive;
    Console.WriteLine(attributes.ToString("X"));          // Displays 00000022                               
    
    
    
  • 相关阅读:
    产品化软件开发与项目化软件开发的对比
    4.ThinkPHP 3.1.2 输出和模型使用
    ThinkPHP 3.1.2 输出和模型使用1
    事务管理配置与@Transactional注解使用
    logstash 区分多个文件index端配置
    logstash 读取多个系统相同文件shipper端
    centos 6.5安装git
    如何查看PHP的配置信息
    MVC模式和URL访问
    1.环境搭建
  • 原文地址:https://www.cnblogs.com/colder/p/3532146.html
Copyright © 2011-2022 走看看