• 令人吐血的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 ;
               
            }


        }
    }

  • 相关阅读:
    清明节实现所有网页变灰色
    点击按钮,复制文本
    Matlab笔记
    spring框架中配置mysql8.0需要注意的地方(转载)
    移动吉比特H2-2光猫超级用户与密码
    JS关闭chorme页面
    MATLAB利用solve函数解多元一次方程组
    微信聊天记录导出为csv,并生成词云图
    fmt.Sprintf(格式化输出)
    iris,context源码分析
  • 原文地址:https://www.cnblogs.com/QinQouShui/p/1374070.html
Copyright © 2020-2023  润新知