您的位置:首页 > 编程语言 > Java开发

常用java字符串操作函数(转)

2011-04-11 15:58 661 查看

[代码]java代码

viewsource

print?

001
/**
002
*分割字符串
003
*
004
*@paramstrString原始字符串
005
*@paramsplitsignString分隔符
006
*@returnString[]分割后的字符串数组
007
*/
008
@SuppressWarnings
(
"unchecked"
)
009
public
static
String[]split(Stringstr,Stringsplitsign){
010
int
index;
011
if
(str==
null
||splitsign==
null
)
012
return
null
;
013
ArrayListal=
new
ArrayList();
014
while
((index=str.indexOf(splitsign))!=-
1
){
015
al.add(str.substring(
0
,index));
016
str=str.substring(index+splitsign.length());
017
}
018
al.add(str);
019
return
(String[])al.toArray(
new
String[
0
]);
020
}
021
022
/**
023
*替换字符串
024
*
025
*@paramfromString原始字符串
026
*@paramtoString目标字符串
027
*@paramsourceString母字符串
028
*@returnString替换后的字符串
029
*/
030
public
static
Stringreplace(Stringfrom,Stringto,Stringsource){
031
if
(source==
null
||from==
null
||to==
null
)
032
return
null
;
033
StringBufferbf=
new
StringBuffer(
""
);
034
int
index=-
1
;
035
while
((index=source.indexOf(from))!=-
1
){
036
bf.append(source.substring(
0
,index)+to);
037
source=source.substring(index+from.length());
038
index=source.indexOf(from);
039
}
040
bf.append(source);
041
return
bf.toString();
042
}
043
044
/**
045
*替换字符串,能能够在HTML页面上直接显示(替换双引号和小于号)
046
*
047
*@paramstrString原始字符串
048
*@returnString替换后的字符串
049
*/
050
public
static
Stringhtmlencode(Stringstr){
051
if
(str==
null
){
052
return
null
;
053
}
054
055
return
replace(
"/""
,
"""
,replace(
"<"
,
"<"
,str));
056
}
057
058
/**
059
*替换字符串,将被编码的转换成原始码(替换成双引号和小于号)
060
*
061
*@paramstrString
062
*@returnString
063
*/
064
public
static
Stringhtmldecode(Stringstr){
065
if
(str==
null
){
066
return
null
;
067
}
068
069
return
replace(
"""
,
"/""
,replace(
"<"
,
"<"
,str));
070
}
071
072
private
static
final
String_BR=
"<br/>"
;
073
074
/**
075
*在页面上直接显示文本内容,替换小于号,空格,回车,TAB
076
*
077
*@paramstrString原始字符串
078
*@returnString替换后的字符串
079
*/
080
public
static
Stringhtmlshow(Stringstr){
081
if
(str==
null
){
082
return
null
;
083
}
084
085
str=replace(
"<"
,
"<"
,str);
086
str=replace(
""
,
" "
,str);
087
str=replace(
"/r/n"
,_BR,str);
088
str=replace(
"/n"
,_BR,str);
089
str=replace(
"/t"
,
"    "
,str);
090
return
str;
091
}
092
093
/**
094
*返回指定字节长度的字符串
095
*
096
*@paramstrString字符串
097
*@paramlengthint指定长度
098
*@returnString返回的字符串
099
*/
100
public
static
StringtoLength(Stringstr,
int
length){
101
if
(str==
null
){
102
return
null
;
103
}
104
if
(length<=
0
){
105
return
""
;
106
}
107
try
{
108
if
(str.getBytes(
"GBK"
).length<=length){
109
return
str;
110
}
111
}
catch
(Exceptionex){
112
}
113
StringBufferbuff=
new
StringBuffer();
114
115
int
index=
0
;
116
char
c;
117
length-=
3
;
118
while
(length>
0
){
119
c=str.charAt(index);
120
if
(c<
128
){
121
length--;
122
}
else
{
123
length--;
124
length--;
125
}
126
buff.append(c);
127
index++;
128
}
129
buff.append(
"..."
);
130
return
buff.toString();
131
}
132
133
/**
134
*判断是否为整数
135
*
136
*@paramstr传入的字符串
137
*@return是整数返回true,否则返回false
138
*/
139
public
static
boolean
isInteger(Stringstr){
140
Patternpattern=Pattern.compile(
"^[-//+]?[//d]*$"
);
141
return
pattern.matcher(str).matches();
142
}
143
144
/**
145
*判断是否为浮点数,包括double和float
146
*
147
*@paramstr传入的字符串
148
*@return是浮点数返回true,否则返回false
149
*/
150
public
static
boolean
isDouble(Stringstr){
151
Patternpattern=Pattern.compile(
"^[-//+]?[.//d]*$"
);
152
return
pattern.matcher(str).matches();
153
}
154
155
/**
156
*判断输入的字符串是否符合Email样式.
157
*
158
*@paramstr传入的字符串
159
*@return是Email样式返回true,否则返回false
160
*/
161
public
static
boolean
isEmail(Stringstr){
162
Patternpattern=Pattern.compile(
"^//w+([-+.]//w+)*@//w+([-.]//w+)*//.//w+([-.]//w+)*$"
);
163
return
pattern.matcher(str).matches();
164
}
165
166
/**
167
*判断输入的字符串是否为纯汉字
168
*
169
*@paramstr传入的字符窜
170
*@return如果是纯汉字返回true,否则返回false
171
*/
172
public
static
boolean
isChinese(Stringstr){
173
Patternpattern=Pattern.compile(
"[/u0391-/uFFE5]+$"
);
174
return
pattern.matcher(str).matches();
175
}
176
177
/**
178
*是否为空白,包括null和""
179
*
180
*@paramstr
181
*@return
182
*/
183
public
static
boolean
isBlank(Stringstr){
184
return
str==
null
||str.trim().length()==
0
;
185
}
186
187
/**
188
*判断是不是合法手机
189
*handset手机号码
190
*/
191
public
static
boolean
isHandset(Stringhandset){
192
try
{
193
if
(!handset.substring(
0
,
1
).equals(
"1"
)){
194
return
false
;
195
}
196
if
(handset==
null
||handset.length()!=
11
){
197
return
false
;
198
}
199
Stringcheck=
"^[0123456789]+$"
;
200
Patternregex=Pattern.compile(check);
201
Matchermatcher=regex.matcher(handset);
202
boolean
isMatched=matcher.matches();
203
if
(isMatched){
204
return
true
;
205
}
else
{
206
return
false
;
207
}
208
}
catch
(RuntimeExceptione){
209
return
false
;
210
}
211
}
212
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: