分享
 
 
 

jakarta开源项目commons-lang2.0笔记----org.apache.commons.lang.StringUtils

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

<文章未完成>

commons-lang2.0是apache中jakarta的commons项目中的一个子项目,其中

org.apache.commons.lang.StringUtils是操纵String类的实用类.

反对部分:

一.

code:

/**

* <p>Strips any of a set of characters from the start and end of a String.

* This is similar to {@link String#trim()} but allows the characters

* to be stripped to be controlled.</p>

*

* <p>A <code>null</code> input String returns <code>null</code>.

* An empty string ("") input returns the empty string.</p>

*

* <p>If the stripChars String is <code>null</code>, whitespace is

* stripped as defined by {@link Character#isWhitespace(char)}.

* Alternatively use {@link #strip(String)}.</p>

*

* <pre>

* StringUtils.strip(null, *) = null

* StringUtils.strip("", *) = ""

* StringUtils.strip("abc", null) = "abc"

* StringUtils.strip(" abc", null) = "abc"

* StringUtils.strip("abc ", null) = "abc"

* StringUtils.strip(" abc ", null) = "abc"

* StringUtils.strip(" abcyx", "xyz") = " abc"

* </pre>

*

* @param str the String to remove characters from, may be null

* @param stripChars the characters to remove, null treated as whitespace

* @return the stripped String, <code>null</code> if null String input

*/

public static String strip(String str, String stripChars) {

if (str == null || str.length() == 0) {

return str;

}

str = stripStart(str, stripChars);

return stripEnd(str, stripChars);

}

/**

* <p>Strips any of a set of characters from the start of a String.</p>

*

* <p>A <code>null</code> input String returns <code>null</code>.

* An empty string ("") input returns the empty string.</p>

*

* <p>If the stripChars String is <code>null</code>, whitespace is

* stripped as defined by {@link Character#isWhitespace(char)}.</p>

*

* <pre>

* StringUtils.stripStart(null, *) = null

* StringUtils.stripStart("", *) = ""

* StringUtils.stripStart("abc", "") = "abc"

* StringUtils.stripStart("abc", null) = "abc"

* StringUtils.stripStart(" abc", null) = "abc"

* StringUtils.stripStart("abc ", null) = "abc "

* StringUtils.stripStart(" abc ", null) = "abc "

* StringUtils.stripStart("yxabc ", "xyz") = "abc "

* </pre>

*

* @param str the String to remove characters from, may be null

* @param stripChars the characters to remove, null treated as whitespace

* @return the stripped String, <code>null</code> if null String input

*/

public static String stripStart(String str, String stripChars) {

int strLen;

if (str == null || (strLen = str.length()) == 0) {

return str;

}

int start = 0;

if (stripChars == null) {

while ((start != strLen) && Character.isWhitespace(str.charAt(start))) {

start++;

}

} else if (stripChars.length() == 0) {

return str;

} else {

while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) != -1)) {

start++;

}

}

return str.substring(start);

}

/**

* <p>Strips any of a set of characters from the end of a String.</p>

*

* <p>A <code>null</code> input String returns <code>null</code>.

* An empty string ("") input returns the empty string.</p>

*

* <p>If the stripChars String is <code>null</code>, whitespace is

* stripped as defined by {@link Character#isWhitespace(char)}.</p>

*

* <pre>

* StringUtils.stripEnd(null, *) = null

* StringUtils.stripEnd("", *) = ""

* StringUtils.stripEnd("abc", "") = "abc"

* StringUtils.stripEnd("abc", null) = "abc"

* StringUtils.stripEnd(" abc", null) = " abc"

* StringUtils.stripEnd("abc ", null) = "abc"

* StringUtils.stripEnd(" abc ", null) = " abc"

* StringUtils.stripEnd(" abcyx", "xyz") = " abc"

* </pre>

*

* @param str the String to remove characters from, may be null

* @param stripChars the characters to remove, null treated as whitespace

* @return the stripped String, <code>null</code> if null String input

*/

public static String stripEnd(String str, String stripChars) {

int end;

if (str == null || (end = str.length()) == 0) {

return str;

}

if (stripChars == null) {

while ((end != 0) && Character.isWhitespace(str.charAt(end - 1))) {

end--;

}

} else if (stripChars.length() == 0) {

return str;

} else {

while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) {

end--;

}

}

return str.substring(0, end);

}

reason:

红色部分下面两个方法中能重用,而且多了次null和""判断(既然string都需要用户来strip,null和""肯定出现概率不高)

my code:

拿去红色部分

二.

code:

/**

* <p>Removes control characters (char &lt;= 32) from both

* ends of this String, handling <code>null</code> by returning

* <code>null</code>.</p>

*

* <p>The String is trimmed using {@link String#trim()}.

* Trim removes start and end characters &lt;= 32.

* To strip whitespace use {@link #strip(String)}.</p>

*

* <p>To trim your choice of characters, use the

* {@link #strip(String, String)} methods.</p>

*

* <pre>

* StringUtils.trim(null) = null

* StringUtils.trim("") = ""

* StringUtils.trim(" ") = ""

* StringUtils.trim("abc") = "abc"

* StringUtils.trim(" abc ") = "abc"

* </pre>

*

* @param str the String to be trimmed, may be null

* @return the trimmed string, <code>null</code> if null String input

*/

public static String trim(String str) {

return (str == null ? null : str.trim());

}

/**

* <p>Removes control characters (char &lt;= 32) from both

* ends of this String returning <code>null</code> if the String is

* empty ("") after the trim or if it is <code>null</code>.

*

* <p>The String is trimmed using {@link String#trim()}.

* Trim removes start and end characters &lt;= 32.

* To strip whitespace use {@link #stripToNull(String)}.</p>

*

* <pre>

* StringUtils.trimToNull(null) = null

* StringUtils.trimToNull("") = null

* StringUtils.trimToNull(" ") = null

* StringUtils.trimToNull("abc") = "abc"

* StringUtils.trimToNull(" abc ") = "abc"

* </pre>

*

* @param str the String to be trimmed, may be null

* @return the trimmed String,

* <code>null</code> if only chars &lt;= 32, empty or null String input

* @since 2.0

*/

public static String trimToNull(String str) {

String ts = trim(str);

return (ts == null || ts.length() == 0 ? null : ts);

}

reason:

有必要这么细的重用颗粒吗?而且还多了次判断,觉得下面这样写比较清晰(实在不能忍受没必要的东西)

my code:

public static String trimToNull(String str) {

if (str == null) {

return null;

}

String ts = str.trim();

return ts.length() == 0 ? null : ts;

}

三.

* <p>Strips whitespace from the start and end of a String returning

* <code>null</code> if the String is empty ("") after the strip.</p>

*

* <p>This is similar to {@link #trimToNull(String)} but removes whitespace.

* Whitespace is defined by {@link Character#isWhitespace(char)}.</p>

*

* <pre>

* StringUtils.strip(null) = null

* StringUtils.strip("") = null

* StringUtils.strip(" ") = null

* StringUtils.strip("abc") = "abc"

* StringUtils.strip(" abc") = "abc"

* StringUtils.strip("abc ") = "abc"

* StringUtils.strip(" abc ") = "abc"

* StringUtils.strip(" ab c ") = "ab c"

* </pre>

*

* @param str the String to be stripped, may be null

* @return the stripped String,

* <code>null</code> if whitespace, empty or null String input

* @since 2.0

*/

public static String stripToNull(String str) {

if (str == null) {

return null;

}

str = strip(str, null);

return (str.length() == 0 ? null : str);

}

reason:

小脚老太婆的裹脚布又来了,难道这个方法和上面的trimToNull不一样吗?当然不排除用户习惯使用strip而不是trim或者历史原因

my code;

和我的trimToNull()一样

(建议自己重写整个lang包时候删除无用的@deprecated以及多余重复的类)

收藏学习部分

一.代码中性能评测记录

// Performance testing notes (JDK 1.4, Jul03, scolebourne)

// Whitespace:

// Character.isWhitespace() is faster than WHITESPACE.indexOf()

// where WHITESPACE is a string of all whitespace characters

//

// Character access:

// String.charAt(n) versus toCharArray(), then array[n]

// String.charAt(n) is about 15% worse for a 10K string

// They are about equal for a length 50 string

// String.charAt(n) is about 4 times better for a length 3 string

// String.charAt(n) is best bet overall

//

// Append:

// String.concat about twice as fast as StringBuffer.append

// (not sure who tested this)

二.算法基本功

如上面的stripEnd()

###############看多少写多少,随时修改###############

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有