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
}
posted on 2008-07-23 18:01
scea2009 阅读(229)
评论(0) 编辑 收藏 所属分类:
网摘