zoukankan      html  css  js  c++  java
  • Copy string.Fromat

    从.net Framework中copy出来的,修改了一下

    public partial class Form1 : Form
        {
            
    public Form1()
            {
                InitializeComponent();
            }

            
    private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(Format(
    null"ss{0}ww{0}ee{1}""AAA""BBB"));

            }

            
    private static void FormatError()
            {
                
    throw new FormatException("参数格式错误"); 
            }

            
    public static string Format(IFormatProvider provider, String format, params Object[] args)
            {
                
    if (format == null || args == null)
                {
                    
    throw new ArgumentNullException((format == null? "format" : "args");
                }
                StringBuilder sb 
    = new StringBuilder();
                
    char[] chars = format.ToCharArray(0, format.Length);
                
    int pos = 0;
                
    int len = chars.Length;
                
    char ch = '\x0';

                ICustomFormatter cf 
    = null;
                
    if (provider != null)
                {
                    cf 
    = (ICustomFormatter)provider.GetFormat(typeof(ICustomFormatter));
                }

                
    while (true)
                {
                    
    int p = pos;
                    
    int i = pos;
                    
    while (pos < len)
                    {
                        ch 
    = chars[pos];

                        pos
    ++;
                        
    if (ch == '}')
                        {
                            
    if (pos < len && chars[pos] == '}'// Treat as escape character for }} 
                                pos++;
                            
    else
                                FormatError();
                        }

                        
    if (ch == '{')
                        {
                            
    if (pos < len && chars[pos] == '{'// Treat as escape character for {{
                                pos++;
                            
    else
                            {
                                pos
    --;
                                
    break;
                            }
                        }

                        chars[i
    ++= ch;
                    }
                    
    if (i > p) sb.Append(chars, p, i - p);
                    
    if (pos == len) break;
                    pos
    ++;
                    
    if (pos == len || (ch = chars[pos]) < '0' || ch > '9') FormatError();
                    
    int index = 0;
                    
    do
                    {
                        index 
    = index * 10 + ch - '0';
                        pos
    ++;
                        
    if (pos == len) FormatError();
                        ch 
    = chars[pos];
                    } 
    while (ch >= '0' && ch <= '9' && index < 1000000);
                    
    if (index >= args.Length) throw new FormatException("索引(从零开始)必须大于或等于零,且小于参数列表的大小。");
                    
    while (pos < len && (ch = chars[pos]) == ' ') pos++;
                    
    bool leftJustify = false;
                    
    int width = 0;
                    
    if (ch == ',')
                    {
                        pos
    ++;
                        
    while (pos < len && chars[pos] == ' ') pos++;

                        
    if (pos == len) FormatError();
                        ch 
    = chars[pos];
                        
    if (ch == '-')
                        {
                            leftJustify 
    = true;
                            pos
    ++;
                            
    if (pos == len) FormatError();
                            ch 
    = chars[pos];
                        }
                        
    if (ch < '0' || ch > '9') FormatError();
                        
    do
                        {
                            width 
    = width * 10 + ch - '0';
                            pos
    ++;
                            
    if (pos == len) FormatError();
                            ch 
    = chars[pos];
                        } 
    while (ch >= '0' && ch <= '9' && width < 1000000);
                    }

                    
    while (pos < len && (ch = chars[pos]) == ' ') pos++;
                    Object arg 
    = args[index];
                    String fmt 
    = null;
                    
    if (ch == ':')
                    {
                        pos
    ++;
                        p 
    = pos;
                        i 
    = pos;
                        
    while (true)
                        {
                            
    if (pos == len) FormatError();
                            ch 
    = chars[pos];
                            pos
    ++;
                            
    if (ch == '{')
                            {
                                
    if (pos < len && chars[pos] == '{')  // Treat as escape character for {{
                                    pos++;
                                
    else
                                    FormatError();
                            }
                            
    else if (ch == '}')
                            {
                                
    if (pos < len && chars[pos] == '}')  // Treat as escape character for }} 
                                    pos++;
                                
    else
                                {
                                    pos
    --;
                                    
    break;
                                }
                            }

                            chars[i
    ++= ch;
                        }
                        
    if (i > p) fmt = new String(chars, p, i - p);
                    }
                    
    if (ch != '}') FormatError();
                    pos
    ++;
                    String s 
    = null;
                    
    if (cf != null)
                    {
                        s 
    = cf.Format(fmt, arg, provider);
                    }

                    
    if (s == null)
                    {
                        
    if (arg is IFormattable)
                        {
                            s 
    = ((IFormattable)arg).ToString(fmt, provider);
                        }
                        
    else if (arg != null)
                        {
                            s 
    = arg.ToString();
                        }
                    }

                    
    if (s == null) s = String.Empty;
                    
    int pad = width - s.Length;
                    
    if (!leftJustify && pad > 0) sb.Append(' ', pad);
                    sb.Append(s);
                    
    if (leftJustify && pad > 0) sb.Append(' ', pad);
                }
                
    return sb.ToString();
            }

        }
  • 相关阅读:
    LeetCode "Jump Game"
    LeetCode "Pow(x,n)"
    LeetCode "Reverse Linked List II"
    LeetCode "Unique Binary Search Trees II"
    LeetCode "Combination Sum II"
    LeetCode "Divide Two Integers"
    LeetCode "First Missing Positive"
    LeetCode "Clone Graph"
    LeetCode "Decode Ways"
    LeetCode "Combinations"
  • 原文地址:https://www.cnblogs.com/jintan/p/1299972.html
Copyright © 2011-2022 走看看