Guava 中文是石榴的意思,该项目是 Google 的一个开源项目,包含许多 Google 核心的 Java 常用库。
目前主要包含:
- com.google.common.annotations
- com.google.common.base
- com.google.common.collect
- com.google.common.io
- com.google.common.net
- com.google.common.primitives
- com.google.common.util.concurrent
代码演示
1
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
import
java.util.ArrayList;
import
java.util.List;
import
com.google.common.base.Joiner;
import
com.google.common.base.Splitter;
import
com.google.common.base.Strings;
import
com.google.common.collect.Lists;
/**
* GUAVE之Strings
* @author WuXiajun
* @date 2013-9-6
*/
public
class
StringTest {
public
static
void
main(String[] args) {
//emptyToNull
System.out.println(
"nullToEmpty"
);
String aa =
null
;
String bb =
""
;
System.out.println(
"aa原始值="
+aa);
System.out.println(
"bb原始值="
+bb);
System.out.println(
"----------"
);
/**
* emptyToNull null和空值都转null
*/
aa = Strings.emptyToNull(aa);
bb = Strings.emptyToNull(bb);
System.out.println(
"转换后"
+aa);
System.out.println(
"转换后"
+bb);
/**
* aa原始值=null
*bb原始值=
*---------
*转换后null
*转换后null
*/
/**
* nullToEmpty NULL或空值都转空值
*/
System.out.println(
"nullToEmpty"
);
String cc =
null
;
String dd =
""
;
System.out.println(
"原CC:"
+cc);
System.out.println(
"原DD:"
+dd);
System.out.println(
"--------"
);
cc = Strings.nullToEmpty(cc);
dd = Strings.nullToEmpty(dd);
System.out.println(
"转后CC"
+cc);
System.out.println(
"转后DD"
+dd);
/**
* 原CC:null
*原DD:
*--------
*转后CC
*转后DD
*/
/**
* isNullOrEmpty判断空值
*/
System.out.println(
"isNullOrEmpty"
);
String s1 =
""
;
String s2 =
null
;
String s3 =
"str"
;
System.out.println(Strings.isNullOrEmpty(s1));
System.out.println(Strings.isNullOrEmpty(s2));
System.out.println(Strings.isNullOrEmpty(s3));
/**
* repeat string
*/
String str1 =
"impll"
;
String strr1 = Strings.repeat(str1,
2
);
System.out.println(
"repeat:"
+strr1);
/**
*repeat:impllimpll
*
*/
/**
* padEnd 在str2后补'a'补够15个长度
*/
String str2 =
"iamachinese."
;
String strr2 = Strings.padEnd(str2,
15
,
'a'
);
System.out.println(strr2);
/**
* iamachinese.aaa
*/
/**
* 同上往前补
*/
String str3 =
"welcome"
;
String strr3 = Strings.padStart(str3,
10
,
'm'
);
System.out.println(strr3);
/**
* 以下两个方法比较相同 commonPrefix从前往后 commonSuffix 从后往前
*/
String strrr = Strings.commonPrefix(
"abcv\uD8AB\uDCABdefx"
,
"abca\uD8AB\uD8ABxyzx"
);
System.out.println(strrr);
strrr = Strings.commonSuffix(
"abcv\uD8AB\uDCABdefx"
,
"abca\uD8AB\uD8ABxyzx"
);
System.out.println(strrr);
/**abc
x
*/
///
//拼接拆分字符串
List<String> strings =
new
ArrayList<String>();
strings.add(
"w"
);
strings.add(
"u"
);
strings.add(
"x"
);
strings.add(
"j"
);
/**java拼接*/
String result =
""
;
if
( strings !=
null
&& !strings.isEmpty() ) {
StringBuilder stringBuilder =
new
StringBuilder();
for
(
int
i =
0
; i < strings.size(); i++ ) {
String string = strings.get( i );
if
( string !=
null
) {
stringBuilder.append( string );
stringBuilder.append(
","
);
}
}
stringBuilder.deleteCharAt( stringBuilder.length() -
1
);
result = stringBuilder.toString();
}
System.out.println(result);
/**Guava拼接*/
Joiner joiner = Joiner.on(
","
).skipNulls();
System.out.println(joiner.join(strings));
/**
* 运行结果都是w,u,x,j Guava强大
*/
String string =
"beijing,qingdao,jinan"
;
/**java拆分*/
strings =
new
ArrayList<String>();
String[] splitted = string.split(
","
);
for
(
int
i =
0
; i < splitted.length; i++ ) {
String element = splitted[ i ].trim();
strings.add( element );
}
System.out.println(strings);
/**guava拆分*/
Iterable<String> split = Splitter.on(
","
).omitEmptyStrings().trimResults().split(string);
strings = Lists.newArrayList(split);
System.out.println(strings);
/**
* 结果都是:[beijing, qingdao, jinan]
*/
}
/**
* 拼接字符串参考
*/
}
|