java.lang.String中的trim()方法的详细说明(转)
String.Trim()方法到底为我们做了什么,仅仅是去除字符串两端的空格吗?
一直以为Trim()方法就是把字符串两端的空格字符给删去,其实我错了,而且错的比较离谱。
首先我直接反编译String类,找到Trim()方法:
public string Trim()
{
return this.TrimHelper(WhitespaceChars, 2);
}
TrimHelper方法有两个参数,第一个参数名WhitespaceChars,首字母尽然是大写的,肯定有文章,真不出我所料:
internal static readonly char[] WhitespaceChars;
这里只是定义它,没有赋值,而且是静态的,我们看看构造函数去,果然找到:
static String()
{ Empty = " "; WhitespaceChars = new char[] { '/t', '/n', '/v', '/f', '/r', ' ', '/x0085', '/x00a0', '?', ' ', ' ', ' ', ' ', '?', '?', '?', '?', '?', ' ', '?', '?', '/u2028', '/u2029', ' ', '?' }; }
继续我们的探索,直接反编译TrimHelper,哇,也许这个才是我想要的,私有的TrimHelper方法:
private string TrimHelper(char[] trimChars, int trimType)
{
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) 去除字符串两端的空白字符。
最后再看看真正执行字符串截取的方法:
private unsafe string InternalSubString(int startIndex, int length, bool fAlwaysCopy)
{
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方法:
TrimStart和TrimEnd方法
如果想去除字符串两端其他任意字符,可以考虑Trim他的重载兄弟:String.Trim(Char[]),传入你想要去除的哪些字符的数组。
源码奉上:
public string Trim(params char[] trimChars)
{
if ((trimChars == null) || (trimChars.Length == 0))
{
trimChars = WhitespaceChars;
}
return this.TrimHelper(trimChars, 2);
}
空格 != 空白字符,删除空格请使用: Trim(‘ ‘);
java.lang.String中的trim()方法的详细说明(转)的更多相关文章
- java.lang.String中的replace方法到底替换了一个还是全部替换了。
你没有看错我说的就是那个最常用的java.lang.String,String可以说在Java中使用量最广泛的类了. 但是我却发现我弄错了他的一个API(也可以说是两个API),这个API是关于字符串 ...
- java.lang.String里面的trim()方法——删除首尾空格
结果如图 package com.softeasy.test1; public class String_trim { public static void main(String[] args) { ...
- java.lang.String 类的所有方法
java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定 ...
- java:常用类(包装类,equals和==的比较,Date,java.lang.String中常用方法,枚举enum)
*包装类: 将基本类型封装成类,其中包含属性和方法以方便对象操作. *byte---->Byte *short--->Short *long--->Long *float---> ...
- java.lang.Math中的基本方法
java.lang.Math类提供的方法都是static的,“静态引入 ”使得不必每次在调用类方法时都在方法前写上类名: import static java.lang.Mat ...
- mybatis invalid comparison: java.sql.Timestamp and java.lang.String报错解决方法
这个错的意思是:java.sql.Timestamp和java.lang.String无效的比较 错误的原因是:拿传入的时间类型参数与空字符串进行比较就会报这个异常 解决方法:只保留非null判断就可 ...
- java基础---->String中的split方法的原理
这里面主要介绍一下关于String类中的split方法的使用以及原理. split函数的说明 split函数java docs的说明: When there is a positive-width m ...
- Java的String中的subString()方法
方法如下: public String substring(int beginIndex, int endIndex) 第一个int为开始的索引,对应String数字中的开始位置, 第二个是截止的索引 ...
- java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String错误的解决方法
mmobjectid是在Oracle数据库中对应的是Number类型的,在JavaBean中定义的是Long类型的. List<BigDecimal> mmobjidAllFromMars ...
随机推荐
- doc下设置永久环境变量的好方法
http://www-2w.blog.163.com/blog/static/97931518201021211123267/ 需要查看命令具体实现:setx machine “%path%”. 配置 ...
- Leetcode 464.我能赢吗
我能赢吗 在 "100 game" 这个游戏中,两名玩家轮流选择从 1 到 10 的任意整数,累计整数和,先使得累计整数和达到 100 的玩家,即为胜者. 如果我们将游戏规则改为 ...
- ASP.NET MVC下使用SWFUpload完成剪切头像功能
首先介绍SWFUpload组件 SWFUpload是一个客户端文件上传工具,最初由Vinterwebb.se开发,它通过整合Flash与JavaScript技术 为WEB开发者提供了一个具有丰富功能继 ...
- Educational Codeforces Round 20 B. Distances to Zero
B. Distances to Zero time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 剑指offer 面试题38
面试题38:数字在排序数组中出现的次数 题目:统计一个数字在排序数组中出现的次数.例如输入排序数组{1,2,3,3,3,3,4,5}和数字3,由于3在这个数组中出现了4次,因此输出4. 主要的思路是进 ...
- 九度oj 题目1397:查找数段
题目描述: 在BaiDu搜索引擎里,如何提高搜索效率是研发人员为之奋斗的目标.现在,JOBDU密码库里也有一段数字片段S(0<长度<=100,000),HQ想通过智能搜索得到包含关键字P( ...
- curl 设置头部
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...
- cronolog切割apache和tomcat日志
cronolog切割apache和tomcat日志 http://cronolog.org tar zxvf cronolog-1.6.2.tar.gzcd cronolog-1.6.2./confi ...
- Hibernate框架简述(转)
转自:http://www.cnblogs.com/eflylab/archive/2007/01/09/615338.html Hibernate的核心组件在基于MVC设计模式的JAVA WEB应用 ...
- cf- 297 < b > -- 区间翻转操作的优化
B. Pasha and String time limit per test 2 seconds memory limit per test 256 megabytes input standard ...