Skip to content

Commit 787e8fc

Browse files
StringUtils的运用
1 parent ee2ba40 commit 787e8fc

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/main/java/com/string/EqualsTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.string;
22

3+
import org.apache.commons.lang.StringUtils;
4+
35
public class EqualsTest {
46
public static void main(String args[]) {
57
String str1="abc1";
@@ -15,5 +17,9 @@ public static void main(String args[]) {
1517
if(str1.toUpperCase().equals(str3.toUpperCase())){
1618
System.out.println(str1+"等于"+str3);
1719
}
20+
//使用StringUtils.equalsIgnoreCase比较大小,既不会空指针,也可以忽略大小写。
21+
if (StringUtils.equalsIgnoreCase(str1, str2)) {
22+
System.out.println("str1等于str2");
23+
}
1824
}
1925
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.string;
2+
3+
/**
4+
* @Author: EnjoyCoding
5+
* @Date: 2020\3\14 0014 22:51
6+
* @Description:
7+
*/
8+
public class SplitDemo {
9+
/**
10+
* 使用索引访问用 String 的 split 方法得到的数组时,需做最后一个分隔符后有无
11+
* 内容的检查,否则会有抛 IndexOutOfBoundsException 的风险。
12+
*/
13+
public void splitStr() {
14+
String str = "a,b,c,,";
15+
String[] ary = str.split(",");
16+
// 预期大于 3,结果是 3
17+
int length=ary.length;
18+
System.out.println(ary[length- 1]);
19+
}
20+
}

0 commit comments

Comments
 (0)