java.lang.String中的trim()方法的详细说明 – muyu114的专栏 – 博客频道 – CSDN.NET

String.Trim()方法到底为我们做了什么,仅仅是去除字符串两端的空格吗?

一直以为Trim()方法就是把字符串两端的空格字符给删去,其实我错了,而且错的比较离谱。

首先我直接反编译String类,找到Trim()方法:

 

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> <b style="color: black; background-color: #a0ffff;">Trim</b>()

{

return this.TrimHelper(WhitespaceChars, 2);

}

 

TrimHelper方法有两个参数,第一个参数名WhitespaceChars,首字母尽然是大写的,肯定有文章,真不出我所料:

 

<span style="color: #0000ff;">internal</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">readonly</span> <span style="color: #0000ff;">char</span>[] WhitespaceChars;

 

这里只是定义它,没有赋值,而且是静态的,我们看看构造函数去,果然找到:

 

<span id="Code_Closed_Text_828048" class="cnblogs_code_Collapse" style="display: none;">String类的构造函数</span><span id="Code_Open_Text_828048" style="display: inline;"><span style="color: #0000ff;">static</span> String()</span>

{

Empty = “ “;

WhitespaceChars = new char[] {

‘/t’, ‘/n’, ‘/v’, ‘/f’, ‘/r’, ‘ ‘, ‘/x0085’, ‘/x00a0’, ‘?’, ‘ ’, ‘ ’, ‘ ’, ‘ ’, ‘?’, ‘?’, ‘?’,

‘?’, ‘?’, ‘ ’, ‘?’, ‘?’, ‘/u2028’, ‘/u2029’, ‘ ’, ‘?’

};

}

 

 

Trim方法就是把字符串两端的这些字符给删去?我很坚定的猜想到。

继续我们的探索,直接反编译TrimHelper,哇,也许这个才是我想要的,私有的TrimHelper方法:

 

<span id="Code_Closed_Text_143441" class="cnblogs_code_Collapse" style="display: none;">TrimHelper方法</span><span id="Code_Open_Text_143441" style="display: inline;"><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">string</span> TrimHelper(<span style="color: #0000ff;">char</span>[] trimChars, <span style="color: #0000ff;">int</span> trimType)</span>

{

int num = this.Length – 1;

int startIndex = 0;

if (trimType != 1)

{

startIndex = 0;

while (startIndex < this.Length)

{

int index = 0;

char ch = this[startIndex];

index = 0;

while (index < trimChars.Length)

{

if (trimChars[index] == ch)

{

break;

}

index++;

}

if (index == trimChars.Length)

{

break;

}

startIndex++;

}

}

if (trimType != 0)

{

num = this.Length – 1;

while (num >= startIndex)

{

int num4 = 0;

char ch2 = this[num];

num4 = 0;

while (num4 < trimChars.Length)

{

if (trimChars[num4] == ch2)

{

break;

}

num4++;

}

if (num4 == trimChars.Length)

{

break;

}

num–;

}

}

int length = (num – startIndex) + 1;

if (length == this.Length)

{

return this;

}

if (length == 0)

{

return Empty;

}

return this.InternalSubString(startIndex, length, false);

}

 

经过分析和运行,基本上知道了这个方法是干什么的了。

TrimHelper方法有两个参数:

第一个参数trimChars,是要从字符串两端删除掉的字符的数组;

第二个参数trimType,是标识Trim的类型。就目前发现,trimType的取值有3个。当传入0时,去除字符串头部的空白字符,传入1时去除字符串尾部的空白字符,传入其他数值(比如2) 去除字符串两端的空白字符。

最后再看看真正执行字符串截取的方法:

 

<span id="Code_Closed_Text_463911" class="cnblogs_code_Collapse" style="display: none;">InternalSubString</span><span id="Code_Open_Text_463911" style="display: inline;"><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">unsafe</span> <span style="color: #0000ff;">string</span> InternalSubString(<span style="color: #0000ff;">int</span> startIndex, <span style="color: #0000ff;">int</span> length, <span style="color: #0000ff;">bool</span> fAlwaysCopy)</span>

{

if (((startIndex == 0) && (length == this.Length)) && !fAlwaysCopy)

{

return this;

}

string str = FastAllocateString(length);

fixed (char* chRef = &str.m_firstChar)

{

fixed (char* chRef2 = &this.m_firstChar)

{

wstrcpy(chRef, chRef2 + startIndex, length);

}

}

return str;

}

 

原来也用指针的?第一次看到,效率应该比较高吧。
最后总结一下:
String.Trim()方法会去除字符串两端,不仅仅是空格字符,它总共能去除25种字符:
(‘/t’, ‘/n’, ‘/v’, ‘/f’, ‘/r’, ‘ ‘, ‘/x0085’, ‘/x00a0’, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘?’, ‘/u2028’, ‘/u2029’, ‘ ‘, ‘?’)
如果你想保留其中的一个或多个(例如/t制表符,/n换行符,/r回车符等),请慎用Trim方法。

请注意,Trim删除的过程为从外到内,直到碰到一个非空白的字符为止,所以不管前后有多少个连续的空白字符都会被删除掉。

最后附上两个相关的方法(也是String类直接提供的),分别去除字符串头部空白字符的TrimStart方法和去除字符串尾部空白字符的 TrimEnd方法:

 

<span id="Code_Closed_Text_775276" class="cnblogs_code_Collapse">TrimStart和TrimEnd方法</span><span id="Code_Open_Text_775276" style="display: none;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> TrimStart(<span style="color: #0000ff;">params</span> <span style="color: #0000ff;">char</span>[] trimChars)</span>

{

if ((trimChars == null) || (trimChars.Length == 0))

{

trimChars = WhitespaceChars;

}

return this.TrimHelper(trimChars, 0);

}

public string TrimEnd(params char[] trimChars)

{

if ((trimChars == null) || (trimChars.Length == 0))

{

trimChars = WhitespaceChars;

}

return this.TrimHelper(trimChars, 1);

}

 

如果想去除字符串两端其他任意字符,可以考虑Trim他的重载兄弟:String.Trim(Char[]),传入你想要去除的哪些字符的数组。

源码奉上:

 

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> <b style="color: black; background-color: #a0ffff;">Trim</b>(<span style="color: #0000ff;">params</span> <span style="color: #0000ff;">char</span>[] trimChars)

{

if ((trimChars == null) || (trimChars.Length == 0))

{

trimChars = WhitespaceChars;

}

return this.TrimHelper(trimChars, 2);

}

 

空格 != 空白字符,删除空格请使用: Trim(‘ ‘);

来源URL:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c422461e002da4b824240d57938327365ff8540abdb6652969423db09ec98e4ad6bc866d72c8713b2340d0014f884fb8cb3763817bcf&p=87769a4787af10f00dbd9b7e0e109d&newp=c36cd615d9c342a906acc7710f578c231610db2151d4db1e2195&user=baidu&fm=sc&query=android+trim&qid=935d95e10000d9d5&p1=2