我想知道C#里string方法里面的Trim()的执行原理。注意是执行原理,不是怎么用。
比如说,它继承自那个接口,用什么方法实现的,实现原理是什么。还望高手赐教一下,呵呵注:标题中的xx译为“蛋疼”

解决方案 »

  1.   

    作用:將所有泛空白字元的項目從這個執行個體的開頭和結尾移除。 命名空間: System組件: mscorlib (在 mscorlib.dll 中)
      

  2.   

    二楼大哥,
    我想知道执行Trim()方法时程序到底是怎么执行的
      

  3.   

    就是循环找到非空白的字符,然后截取子串。源码:public string Trim()
    {
        return this.TrimHelper(2);
    }private string TrimHelper(int trimType)
    {
        int end = this.Length - 1;
        int start = 0;
        if (trimType != 1)
        {
            start = 0;
            while (start < this.Length)
            {
                if (!char.IsWhiteSpace(this[start]))
                {
                    break;
                }
                start++;
            }
        }
        if (trimType != 0)
        {
            end = this.Length - 1;
            while (end >= start)
            {
                if (!char.IsWhiteSpace(this[end]))
                {
                    break;
                }
                end--;
            }
        }
        return this.CreateTrimmedString(start, end);
    }
      

  4.   

    internal static readonly char[] WhitespaceChars =   
            { (char) 0x9, (char) 0xA, (char) 0xB, (char) 0xC, (char) 0xD, (char) 0x20,   (char) 0x85, 
              (char) 0xA0, (char)0x1680,
              (char) 0x2000, (char) 0x2001, (char) 0x2002, (char) 0x2003, (char) 0x2004, (char) 0x2005,
              (char) 0x2006, (char) 0x2007, (char) 0x2008, (char) 0x2009, (char) 0x200A, (char) 0x200B,
              (char) 0x2028, (char) 0x2029,
              (char) 0x3000, (char) 0xFEFF };    public String Trim(params char[] trimChars) {
            if (null==trimChars || trimChars.Length == 0) {
                trimChars=WhitespaceChars;
            }
            return TrimHelper(trimChars,TrimBoth);
        }    public String TrimStart(params char[] trimChars) {
            if (null==trimChars || trimChars.Length == 0) {
                trimChars=WhitespaceChars;
            }
            return TrimHelper(trimChars,TrimHead);
        }    public String TrimEnd(params char[] trimChars) {
            if (null==trimChars || trimChars.Length == 0) {
                trimChars=WhitespaceChars;
            }
            return TrimHelper(trimChars,TrimTail);
        }    public String Trim() {
            return TrimHelper(WhitespaceChars,TrimBoth);        
        }    private String TrimHelper(char[] trimChars, int trimType) {
            int end = this.Length-1;
            int start=0;        if (trimType !=TrimTail)  {
                for (start=0; start < this.Length; start++) {
                    int i = 0;
                    char ch = this[start];
                    for( i = 0; i < trimChars.Length; i++) {
                        if( trimChars[i] == ch) break;
                    }
                    if( i == trimChars.Length) {
                        break;  
                    }
                }
            }
            
            if (trimType !=TrimHead) {
                for (end= Length -1; end >= start;  end--) {
                    int i = 0;    
                    char ch = this[end];                    
                    for(i = 0; i < trimChars.Length; i++) {
                        if( trimChars[i] == ch) break;
                    }
                    if( i == trimChars.Length) { 
                        break;  
                    }                    
                }
            }
            int len = end -start + 1;
            if (len == this.Length) {
                return this;
            }
            else {
                if( len == 0) {
                    return String.Empty;
                }
                return InternalSubString(start, len, false);
            }
        }   
      

  5.   

    我仅仅是想知道Trim这个最简单的方法到底是怎么写的,我也没什么用处,就是忽然想知道罢了,呵呵
      

  6.   

    都告诉你是那个dll里的,你跟着这个线索反编译一下去看就可以了。
      

  7.   

    - -|||
    直接string.replace(" ","");
    就相当于trim()勒。
      

  8.   

    非也,Trim()只去前后空格
    你这把所有空格都去了