1
/**
2
* Title: Java Bean 工具
3
* Description:
4
* Copyright: Copyright (c) 2001
5
* Company: JAVA世紀網 http://www.java2000.net
6
* @author 趙學慶
7
* @version 1.0
8
*/
9
import java.util.*;
10
import java.util.regex.Pattern;
11
12
public class StrTools {
13
/**
14
* 分割字符串
15
*
16
* @param str String 原始字符串
17
* @param splitsign String 分隔符
18
* @return String[] 分割后的字符串數組
19
*/
20
@SuppressWarnings("unchecked")
21
public static String[] split(String str, String splitsign) {
22
int index;
23
if (str == null || splitsign == null)
24
return null;
25
ArrayList al = new ArrayList();
26
while ((index = str.indexOf(splitsign)) != -1) {
27
al.add(str.substring(0, index));
28
str = str.substring(index + splitsign.length());
29
}
30
al.add(str);
31
return (String[]) al.toArray(new String[0]);
32
}
33
34
/**
35
* 替換字符串
36
*
37
* @param from String 原始字符串
38
* @param to String 目標字符串
39
* @param source String 母字符串
40
* @return String 替換后的字符串
41
*/
42
public static String replace(String from, String to, String source) {
43
if (source == null || from == null || to == null)
44
return null;
45
StringBuffer bf = new StringBuffer("");
46
int index = -1;
47
while ((index = source.indexOf(from)) != -1) {
48
bf.append(source.substring(0, index) + to);
49
source = source.substring(index + from.length());
50
index = source.indexOf(from);
51
}
52
bf.append(source);
53
return bf.toString();
54
}
55
56
/**
57
* 替換字符串,能能夠在HTML頁面上直接顯示(替換雙引號和小于號)
58
*
59
* @param str String 原始字符串
60
* @return String 替換后的字符串
61
*/
62
public static String htmlencode(String str) {
63
if (str == null) {
64
return null;
65
}
66
67
return replace("\"", """, replace("<", "<", str));
68
}
69
70
/**
71
* 替換字符串,將被編碼的轉換成原始碼(替換成雙引號和小于號)
72
*
73
* @param str String
74
* @return String
75
*/
76
public static String htmldecode(String str) {
77
if (str == null) {
78
return null;
79
}
80
81
return replace(""", "\"", replace("<", "<", str));
82
}
83
84
private static final String _BR = "<br/>";
85
86
/**
87
* 在頁面上直接顯示文本內容,替換小于號,空格,回車,TAB
88
*
89
* @param str String 原始字符串
90
* @return String 替換后的字符串
91
*/
92
public static String htmlshow(String str) {
93
if (str == null) {
94
return null;
95
}
96
97
str = replace("<", "<", str);
98
str = replace(" ", " ", str);
99
str = replace("\r\n", _BR, str);
100
str = replace("\n", _BR, str);
101
str = replace("\t", " ", str);
102
return str;
103
}
104
105
/**
106
* 返回指定字節長度的字符串
107
*
108
* @param str String 字符串
109
* @param length int 指定長度
110
* @return String 返回的字符串
111
*/
112
public static String toLength(String str, int length) {
113
if (str == null) {
114
return null;
115
}
116
if (length <= 0) {
117
return "";
118
}
119
try {
120
if (str.getBytes("GBK").length <= length) {
121
return str;
122
}
123
} catch (Exception ex) {
124
}
125
StringBuffer buff = new StringBuffer();
126
127
int index = 0;
128
char c;
129
length -= 3;
130
while (length > 0) {
131
c = str.charAt(index);
132
if (c < 128) {
133
length--;
134
} else {
135
length--;
136
length--;
137
}
138
buff.append(c);
139
index++;
140
}
141
buff.append("
");
142
return buff.toString();
143
}
144
145
/**
146
* 判斷是否為整數
147
*
148
* @param str 傳入的字符串
149
* @return 是整數返回true,否則返回false
150
*/
151
public static boolean isInteger(String str) {
152
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
153
return pattern.matcher(str).matches();
154
}
155
156
/**
157
* 判斷是否為浮點數,包括double和float
158
*
159
* @param str 傳入的字符串
160
* @return 是浮點數返回true,否則返回false
161
*/
162
public static boolean isDouble(String str) {
163
Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
164
return pattern.matcher(str).matches();
165
}
166
167
/**
168
* 判斷輸入的字符串是否符合Email樣式.
169
*
170
* @param str 傳入的字符串
171
* @return 是Email樣式返回true,否則返回false
172
*/
173
public static boolean isEmail(String str) {
174
Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
175
return pattern.matcher(str).matches();
176
}
177
178
/**
179
* 判斷輸入的字符串是否為純漢字
180
*
181
* @param str 傳入的字符竄
182
* @return 如果是純漢字返回true,否則返回false
183
*/
184
public static boolean isChinese(String str) {
185
Pattern pattern = Pattern.compile("[\u0391-\uFFE5]+$");
186
return pattern.matcher(str).matches();
187
}
188
189
/**
190
* 是否為空白,包括null和""
191
*
192
* @param str
193
* @return
194
*/
195
public static boolean isBlank(String str) {
196
return str == null || str.trim().length() == 0;
197
}
198
199
/**
200
* 判斷是否為質數
201
*
202
* @param x
203
* @return
204
*/
205
public static boolean isPrime(int x) {
206
if (x <= 7) {
207
if (x == 2 || x == 3 || x == 5 || x == 7)
208
return true;
209
}
210
int c = 7;
211
if (x % 2 == 0)
212
return false;
213
if (x % 3 == 0)
214
return false;
215
if (x % 5 == 0)
216
return false;
217
int end = (int) Math.sqrt(x);
218
while (c <= end) {
219
if (x % c == 0) {
220
return false;
221
}
222
c += 4;
223
if (x % c == 0) {
224
return false;
225
}
226
c += 2;
227
if (x % c == 0) {
228
return false;
229
}
230
c += 4;
231
if (x % c == 0) {
232
return false;
233
}
234
c += 2;
235
if (x % c == 0) {
236
return false;
237
}
238
c += 4;
239
if (x % c == 0) {
240
return false;
241
}
242
c += 6;
243
if (x % c == 0) {
244
return false;
245
}
246
c += 2;
247
if (x % c == 0) {
248
return false;
249
}
250
c += 6;
251
}
252
return true;
253
}
254
255
public static void main(String[] args) {
256
String[] numbers = { "12345", "-12345", "123.45", "-123.45", ".12345", "-.12345", "a12345", "12345a", "123.a45" };
257
for (String str : numbers) {
258
System.out.println(str + "=" + isInteger(str) + " " + isDouble(str));
259
}
260
261
String[] emails = { "1@2.com", "1.2@3.com", "1@3.4.5.com" };
262
for (String str : emails) {
263
System.out.println(str + "=" + isEmail(str));
264
}
265
String[] chineses = { "中國", "1中國", "中國1", "1中國2", "中1國" };
266
for (String str : chineses) {
267
System.out.println(str + "=" + isChinese(str));
268
}
269
}
270
}

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

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270
