zoukankan      html  css  js  c++  java
  • 令人吐血的string.format 对齐问题

                   //可以对齐           

                Console.WriteLine("{0,-20}{1}", "fafdaafd奔号:", "213");
                Console.WriteLine("{0,-20}{1}", "fdfaa奔号:", "13");
                //增加一个英文字母t,可以对齐
                Console.WriteLine("{0,-20}{1}", "fafdatafd奔号:", "213");
                Console.WriteLine("{0,-20}{1}", "fdfaa奔号:", "13");
               
                //增加一个汉字减少两个字母,可以对齐
                Console.WriteLine("{0,-20}{1}", "fafdfd我奔号:", "213");
                Console.WriteLine("{0,-20}{1}", "fdfaa奔号:", "13");

                //增加一个汉字减少一个字母,不可以对齐
                Console.WriteLine("{0,-20}{1}", "fafdaafd我奔号:", "213");
                Console.WriteLine("{0,-20}{1}", "fdfaa奔号:", "13");

    以下为控制台输出的结果

    以下为消息框的显示:

    处理方法 :

     

     

     

     

    public class QString 
        {
            string s;
            /// <summary>
            /// 新建实例
            /// </summary>
            /// <param name="s"></param>
            public QString(string s)
            {
                this.s = s;
            }

           
            /// <summary>
            /// 待格式化的
            /// </summary>
             /// <param name="length">长度</param>
            /// <param name="chr">补码</param>
            /// <returns>格式化的数据</returns>
            /// <remarks>
            /// 以英文字母为基准,根据最大长度与实际长度计算补码长度
            /// 如:c 20
            ///     c我 20-1
            ///     c多我我 20-3
            /// </remarks>
            public   string PadLeft(  int length, char chr)
            {

                return s.PadLeft(length  - new System.Text.RegularExpressions.Regex(@"[\u4e00-\u9fa5]").Matches(s).Count, chr);

            }


            /// <summary>
            /// 待格式化的
            /// </summary>
            /// <param name="length">长度</param>
            /// <param name="chr">补码</param>
            /// <returns>格式化的数据</returns>
            /// <remarks>
            /// 以英文字母为基准,根据最大长度与实际长度计算补码长度
            /// 如:c 20
            ///     c我 20-1
            ///     c多我我 20-3
            /// </remarks>
            public string PadRight(int length, char chr)
            {

                return s.PadRight(length - new System.Text.RegularExpressions.Regex(@"[\u4e00-\u9fa5]").Matches(s).Count, chr);

            }


        }

    2009-10-29

    经过大量实践,能在比较多的场合使用,但MSGBOX不适用。

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace QouShui.DLL
    {
        public class QString 
        {
            string s;
            /// <summary>
            /// 新建实例
            /// </summary>
            /// <param name="s"></param>
            public QString(string s)
            {
                this.s = s;
                hcount = reg.Matches(s).Count ;
            }

            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"[\uFF00-\uFFFF\u4e00-\u9fa5]");
            int hcount = 0;

            /// <summary>
            /// 待格式化的
            /// </summary>
             /// <param name="length">长度</param>
            /// <param name="chr">补码</param>
            /// <returns>格式化的数据</returns>
            /// <remarks>
            /// 以英文字母为基准,根据最大长度与实际长度计算补码长度
            /// 如:c 20
            ///     c我 20-1
            ///     c多我我 20-3
            /// </remarks>
            public string PadLeft(int length, char chr)
            {
                if (length - s.Length - reg.Matches(s).Count < 0)
                    length = s.Length + reg.Matches(s).Count;
               
                return s + new string(chr, length - s.Length - reg.Matches(s).Count);
            }


            /// <summary>
            /// 待格式化的
            /// </summary>
            /// <param name="length">长度</param>
            /// <param name="chr">补码</param>
            /// <returns>格式化的数据</returns>
            /// <remarks>
            /// 以英文字母为基准,根据最大长度与实际长度计算补码长度
            /// 如:c 20
            ///     c我 20-1
            ///     c多我我 20-3
            /// </remarks>
            public string PadRight(int length, char chr)
            {
                if (length - s.Length - reg.Matches(s).Count < 0)
                    length = s.Length + reg.Matches(s).Count;
               
               return  new string(chr, length - s.Length - reg.Matches(s).Count) + s ;
               
            }


        }
    }

  • 相关阅读:
    windows的80端口被占用时的处理方法
    Ansible自动化运维工具安装与使用实例
    Tomcat的测试网页换成自己项目首页
    LeetCode 219. Contains Duplicate II
    LeetCode Contest 177
    LeetCode 217. Contains Duplicate
    LeetCode 216. Combination Sum III(DFS)
    LeetCode 215. Kth Largest Element in an Array(排序)
    Contest 176 LeetCode 1354. Construct Target Array With Multiple Sums(优先队列,递推)
    Contest 176
  • 原文地址:https://www.cnblogs.com/QinQouShui/p/1374070.html
Copyright © 2011-2022 走看看