Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
X
xxl-job
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
刘晓滨
xxl-job
Commits
9819d87d
Commit
9819d87d
authored
Dec 03, 2018
by
刘晓滨
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
StringUtil
parent
9ceaa704
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
1959 additions
and
0 deletions
+1959
-0
StringUtil.java
...-core/src/main/java/com/xxl/job/core/util/StringUtil.java
+1959
-0
No files found.
xxl-job-core/src/main/java/com/xxl/job/core/util/StringUtil.java
0 → 100644
View file @
9819d87d
package
com
.
xxl
.
job
.
core
.
util
;
import
java.io.UnsupportedEncodingException
;
import
java.math.BigDecimal
;
import
java.math.BigInteger
;
import
java.text.DecimalFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Calendar
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Random
;
import
java.util.UUID
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
javax.script.ScriptEngine
;
import
javax.script.ScriptEngineManager
;
import
javax.script.ScriptException
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
public
class
StringUtil
{
private
final
static
DecimalFormat
decimalFormat
=
new
DecimalFormat
(
"0.00"
);
private
final
static
DecimalFormat
DECIMAL_FORMAT_FOUR
=
new
DecimalFormat
(
"0.0000"
);
public
static
final
String
MSG_NEWLINE
=
"\n"
;
public
static
final
String
MSG_SPACE
=
" "
;
public
static
final
String
RETURN_MSG_SAVE_SUCCESS
=
"保存成功"
;
public
static
final
String
RETURN_MSG_UPDATE_SUCCESS
=
"更新成功"
;
public
static
final
String
RETURN_MSG_FIND_NO_DATA
=
"没有数据"
;
public
static
final
String
RETURN_MSG_DELETE_FAIL
=
"删除失败"
;
public
static
final
String
RETURN_MSG_DELETE_SUCCESS
=
"删除成功"
;
public
static
final
String
RETURN_MSG_EXPORT_FAIL
=
"导出失败"
;
public
static
final
String
RETURN_MSG_SUCCESS
=
"success"
;
public
static
final
String
RETURN_MSG_FAIL
=
"fail"
;
public
static
final
String
[]
CHINESE_NUMBER
=
new
String
[]
{
"一"
,
"二"
,
"三"
,
"四"
,
"五"
,
"六"
,
"七"
,
"八"
,
"九"
,
"十"
,
"十一"
,
"十二"
,
"十三"
,
"十四"
,
"十五"
};
public
static
final
int
NUMBER_SCALE
=
2
;
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
StringUtil
.
class
);
static
Random
r
=
new
Random
();
private
static
double
EARTH_RADIUS
=
6378.137
;
// 地球半径
private
StringUtil
()
{
};
public
static
String
getURLEncoderString
(
String
str
)
{
String
result
=
"http:/"
;
String
pureurl
=
str
.
substring
(
7
,
str
.
length
());
String
[]
urls
=
pureurl
.
split
(
"/"
);
for
(
String
url
:
urls
)
{
try
{
result
+=
"/"
+
java
.
net
.
URLEncoder
.
encode
(
url
,
"GBK"
);
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
}
}
return
result
;
}
// public static String encodeBASE64(String s) {
// if (s == null)
// return null;
// try {
// return new String(Base64.encodeBase64(s.getBytes()), "UTF-8");
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// return null;
// }
// // return Base64.encodeBase64String(s.getBytes());
// // return (new BASE64Encoder()).encode(s.getBytes());
// }
public
static
long
getRandom18Long
()
{
return
System
.
currentTimeMillis
()
*
100000
+
r
.
nextInt
(
90000
)
+
10000
;
}
/**
* 将BASE64字符串进行解密
*
* @param bytes
* @return
*/
// public static String decodeBASE64(String s) {
// if (s == null)
// return null;
//
// try {
// byte[] b = Base64.decodeBase64(s.getBytes("UTF-8"));
// ;
// return new String(b);
// } catch (Exception e) {
// e.printStackTrace();
// return null;
// }
// }
public
static
<
E
>
List
<
E
>
toList
(
Iterable
<
E
>
iterable
)
{
if
(
iterable
instanceof
List
)
{
return
(
List
<
E
>)
iterable
;
}
ArrayList
<
E
>
list
=
new
ArrayList
<
E
>();
if
(
iterable
!=
null
)
{
for
(
E
e
:
iterable
)
{
list
.
add
(
e
);
}
}
return
list
;
}
/**
* 判断字符串是否为空
*/
public
static
boolean
isNullOrEmpty
(
String
str
)
{
if
(
str
==
null
||
str
.
length
()
<=
0
||
str
.
trim
()
==
null
||
str
.
trim
().
length
()
<=
0
)
{
return
true
;
}
return
false
;
}
/**
* 判断字符串是否不为空
*/
public
static
boolean
isNotEmpty
(
String
str
)
{
return
!
isNullOrEmpty
(
str
);
}
/**
* 字符串首字母转大写
*
* @param string
* @return
*/
public
static
String
firstLetterToStringUpperCase
(
String
string
)
{
return
string
.
substring
(
0
,
1
).
toUpperCase
()
+
string
.
substring
(
1
);
}
/**
* @param args
*/
public
static
int
getIntOfStr
(
String
str
)
{
try
{
return
new
Integer
(
str
).
intValue
();
}
catch
(
Exception
e
)
{
return
0
;
}
}
/**
* 处理 1,2格式的字符串,处理为 '1','2'
*
* @return
*/
public
static
String
processSplitStr
(
String
splitStr
)
{
StringBuffer
sb
=
new
StringBuffer
();
String
[]
splitStrs
=
splitStr
.
split
(
","
);
sb
.
append
(
"'"
).
append
(
splitStrs
[
0
]).
append
(
"'"
);
for
(
int
i
=
1
;
i
<
splitStrs
.
length
;
i
++)
{
sb
.
append
(
",'"
).
append
(
splitStrs
[
i
]).
append
(
"'"
);
}
return
sb
.
toString
();
}
/**
* @param args
*/
public
static
int
getIntOfObj
(
Object
obj
)
{
try
{
return
new
Integer
(
obj
.
toString
()).
intValue
();
}
catch
(
Exception
e
)
{
return
0
;
}
}
public
static
boolean
getBooleanOfObj
(
Object
object
)
{
try
{
return
(
Boolean
)
object
;
}
catch
(
Exception
e
)
{
return
false
;
}
}
public
static
boolean
getBooleanOfInt
(
int
i
)
{
return
i
==
0
?
false
:
true
;
}
/**
* @param args
*/
public
static
long
getLongOfObj
(
Object
obj
)
{
try
{
return
new
Long
(
obj
.
toString
()).
longValue
();
}
catch
(
Exception
e
)
{
return
0
;
}
}
/**
* @param args
*/
public
static
double
getDoubleOfObj
(
Object
obj
)
{
try
{
return
new
Double
(
obj
.
toString
()).
doubleValue
();
}
catch
(
Exception
e
)
{
return
0
;
}
}
/**
* @param args
* @return 如果转换失败,返回-2
*/
public
static
float
getFloatOfObj
(
Object
obj
)
{
try
{
return
new
Float
(
obj
.
toString
()).
floatValue
();
}
catch
(
Exception
e
)
{
return
-
2
;
}
}
/**
* @param args
* @return 如果转换失败,返回-2
*/
public
static
float
getNullFloat
(
Object
obj
)
{
try
{
return
new
Float
(
obj
.
toString
()).
floatValue
();
}
catch
(
Exception
e
)
{
return
0
;
}
}
/**
* 将浮点型保留两位小数
*/
public
static
String
formartDecimal
(
Object
d
)
{
try
{
String
str
=
new
java
.
text
.
DecimalFormat
(
"#.00"
).
format
(
d
);
if
(
str
.
charAt
(
0
)
==
'.'
)
// 0.01 格式化为 .01
{
str
=
"0"
+
str
;
}
if
(
str
.
indexOf
(
"-."
)
==
0
)
// -.01 开头
{
str
=
"-0."
+
str
.
substring
(
2
);
}
if
(
str
.
equals
(
"0"
)
||
str
.
equals
(
"0.00"
))
{
str
=
"0"
;
}
return
str
.
replace
(
"-0.00"
,
""
).
replace
(
".00"
,
""
);
}
catch
(
Exception
e
)
{
return
""
;
}
}
/**
* @param args
*/
public
static
int
[]
getIntOfObjArry
(
Object
[]
objs
)
{
int
[]
result
=
new
int
[
objs
.
length
];
for
(
int
i
=
0
;
i
<
objs
.
length
;
i
++)
{
result
[
i
]
=
getIntOfObj
(
objs
[
i
]);
}
return
result
;
}
/**
* 返回对象字符串,对于出错情况返回空字符串
*
* @param obj
* @return
*/
public
static
String
toString
(
Object
obj
)
{
try
{
return
obj
==
null
?
""
:
obj
.
toString
();
}
catch
(
Exception
e
)
{
return
""
;
}
}
/**
* @param args
*/
public
static
String
getNullStr
(
Object
obj
)
{
try
{
return
obj
.
toString
();
}
catch
(
Exception
e
)
{
return
""
;
}
}
public
static
String
filterEmoji
(
String
source
)
{
if
(
StringUtil
.
isNotEmpty
(
source
))
{
Pattern
emoji
=
Pattern
.
compile
(
"[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]"
,
Pattern
.
UNICODE_CASE
|
Pattern
.
CASE_INSENSITIVE
);
Matcher
emojiMatcher
=
emoji
.
matcher
(
source
);
if
(
emojiMatcher
.
find
())
{
source
=
emojiMatcher
.
replaceAll
(
""
);
return
source
;
}
return
source
;
}
return
source
;
}
/**
* @param args
*/
public
static
String
getTrimStr
(
String
obj
)
{
try
{
return
obj
.
trim
();
}
catch
(
Exception
e
)
{
return
""
;
}
}
/**
* @param args
* 将以逗号隔开的字符串转换为集合
*/
public
static
List
<
Integer
>
getNullStrToList
(
String
str
)
{
String
num
[]
=
str
.
split
(
","
);
List
<
Integer
>
stateList
=
new
ArrayList
<
Integer
>();
if
(
StringUtil
.
isNotEmpty
(
str
)
&&
num
.
length
>
0
&&
!
str
.
equals
(
"null"
))
{
for
(
int
i
=
0
;
i
<
num
.
length
;
i
++)
{
String
value
=
StringUtil
.
getNullStr
(
num
[
i
]).
trim
();
stateList
.
add
(
Integer
.
parseInt
(
value
));
}
}
else
{
stateList
.
add
(
0
);
}
return
stateList
;
}
/**
* 除去字符串里的] [ "
*
* @param str
* ,
* @return
*/
public
static
String
getNullStrToNoAsteriskAndBrackets
(
String
str
)
{
if
(
StringUtil
.
isNotEmpty
(
str
)
&&
!
str
.
equals
(
"null"
))
{
str
=
str
.
replace
(
"["
,
""
);
str
=
str
.
replace
(
"\""
,
""
);
str
=
str
.
replace
(
"]"
,
""
);
}
else
{
str
=
""
;
}
return
str
;
}
/**
* @param args
* 将以逗号隔开的字符串转换为集合
*/
public
static
List
<
String
>
getNullStrToStringList
(
String
str
)
{
String
num
[]
=
str
.
split
(
","
);
List
<
String
>
stateList
=
new
ArrayList
<
String
>();
if
(
StringUtil
.
isNotEmpty
(
str
)
&&
num
.
length
>
0
&&
!
str
.
equals
(
"null"
))
{
for
(
int
i
=
0
;
i
<
num
.
length
;
i
++)
{
stateList
.
add
(
num
[
i
]);
}
}
else
{
stateList
.
add
(
""
);
}
return
stateList
;
}
/**
* @param args
* 将以指定的字符隔开的字符串转换为集合
*/
public
static
List
<
String
>
getNullStrToStringListByOperator
(
String
str
,
String
operator
)
{
String
num
[]
=
str
.
split
(
operator
);
List
<
String
>
stateList
=
new
ArrayList
<
String
>();
if
(
StringUtil
.
isNotEmpty
(
str
)
&&
num
.
length
>
0
&&
!
str
.
equals
(
"null"
))
{
for
(
int
i
=
0
;
i
<
num
.
length
;
i
++)
{
stateList
.
add
(
num
[
i
]);
}
}
else
{
stateList
.
add
(
""
);
}
return
stateList
;
}
/**
* @param args
* 将以逗号隔开的字符串转换为集合
*/
public
static
List
<
Long
>
getNullStrToLongList
(
String
str
)
{
List
<
Long
>
stateList
=
new
ArrayList
<
Long
>();
if
(
StringUtil
.
isNotEmpty
(
str
))
{
String
num
[]
=
str
.
split
(
","
);
if
(
num
.
length
>
0
)
{
for
(
int
i
=
0
;
i
<
num
.
length
;
i
++)
{
stateList
.
add
(
StringUtil
.
getNullLong
(
num
[
i
]));
}
}
}
else
{
stateList
.
add
(
0L
);
}
return
stateList
;
}
/**
* @param args
* ,SQL查询时 like setParameter 加上%%
*/
public
static
String
getNullStrLike
(
Object
obj
)
{
try
{
return
"%"
+
obj
.
toString
()
+
"%"
;
}
catch
(
Exception
e
)
{
return
"%"
+
""
+
"%"
;
}
}
/**
* @param args
* ,SQL查询时 like setParameter 加上%
*/
public
static
String
getNullStrLikeSineRight
(
Object
obj
)
{
try
{
return
obj
.
toString
()
+
"%"
;
}
catch
(
Exception
e
)
{
return
""
+
"%"
;
}
}
/**
* @param args
*/
public
static
Long
getNullLong
(
Object
obj
)
{
try
{
return
Long
.
valueOf
(
obj
.
toString
());
}
catch
(
Exception
e
)
{
return
0L
;
}
}
/**
* 自动将字符串补足10位 ,如1111——》0000001111
*
* @param kh
* @return
*/
public
static
String
fixToTen
(
String
kh
)
{
if
(
StringUtil
.
isNotEmpty
(
kh
)
&&
kh
.
length
()
<
10
)
{
for
(
int
i
=
10
-
kh
.
length
();
i
>
0
;
i
--)
{
kh
=
"0"
+
kh
;
}
}
return
kh
;
}
// public static String getIdGenerator() throws BusinessException {
// String idEntity = "";
// try {
// idEntity = DateUtil.getFormatDate(new Date());
// int random = (int) (Math.random() * 90000000) + 10000000;// 生成8位随机数
// String randoms = String.valueOf(random);
// idEntity = idEntity + randoms;
// // log.info("id=" + idEntity);
// } catch (Exception e) {
// e.printStackTrace();
// throw new BusinessException("主键生成失败");
// }
// return idEntity;
// }
// public static Long getBigIntId() {
// return getNullLong(getIdGenerator());
// }
/**
* System.currentTimeMillis+10位随机字符
*
* @return
*/
public
static
String
randomstr
()
{
StringBuffer
sb
=
new
StringBuffer
();
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
int
intVal
=
(
int
)
(
Math
.
random
()
*
26
+
97
);
sb
.
append
((
char
)
intVal
);
}
return
"YEY_"
+
System
.
currentTimeMillis
()
+
sb
.
toString
();
}
public
static
String
getWeekStr
(
int
day
)
{
String
weekStr
=
null
;
switch
(
day
)
{
case
1
:
weekStr
=
"星期一"
;
break
;
case
2
:
weekStr
=
"星期二"
;
break
;
case
3
:
weekStr
=
"星期三"
;
break
;
case
4
:
weekStr
=
"星期四"
;
break
;
case
5
:
weekStr
=
"星期五"
;
break
;
case
6
:
weekStr
=
"星期六"
;
break
;
case
7
:
weekStr
=
"星期日"
;
break
;
}
return
weekStr
;
}
/**
* 产生6位随机数,不足补零
*
* @return
*/
public
static
String
get6RandomStr
()
{
int
result
=
0
;
Random
rand
=
new
Random
();
result
=
rand
.
nextInt
(
999999
);
return
"000000"
.
substring
((
result
+
""
).
length
())
+
result
;
}
public
static
String
removeTagFromText
(
String
content
)
{
if
(
StringUtil
.
isNullOrEmpty
(
content
))
{
return
""
;
}
Pattern
p
=
null
;
Matcher
m
=
null
;
String
value
=
null
;
// 去掉<>标签
p
=
Pattern
.
compile
(
"(<[^>]*>)"
);
m
=
p
.
matcher
(
content
);
String
temp
=
content
;
while
(
m
.
find
())
{
value
=
m
.
group
(
0
);
temp
=
temp
.
replace
(
value
,
""
);
}
// 去掉换行或回车符号
p
=
Pattern
.
compile
(
"(/r+|/n+)"
);
m
=
p
.
matcher
(
temp
);
while
(
m
.
find
())
{
value
=
m
.
group
(
0
);
temp
=
temp
.
replace
(
value
,
" "
);
}
if
(
StringUtil
.
isNotEmpty
(
temp
))
{
temp
=
temp
.
replaceAll
(
" "
,
""
);
}
if
(
StringUtil
.
isNotEmpty
(
temp
))
{
temp
=
temp
.
replaceAll
(
"\n"
,
""
);
temp
=
temp
.
replaceAll
(
"\t"
,
""
);
}
if
(
StringUtil
.
isNotEmpty
(
temp
))
{
temp
=
temp
.
replaceAll
(
" "
,
""
);
}
return
temp
;
}
/**
* 去掉<>标签
*
* @param content
* @return
*/
public
static
String
removeHtmlTagFromText
(
String
content
)
{
try
{
Pattern
p
=
null
;
Matcher
m
=
null
;
String
value
=
null
;
// 去掉<>标签
p
=
Pattern
.
compile
(
"(<[^>]*>)"
);
m
=
p
.
matcher
(
content
);
String
temp
=
content
;
while
(
m
.
find
())
{
value
=
m
.
group
(
0
);
temp
=
temp
.
replace
(
value
,
""
);
}
return
temp
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
content
;
}
}
public
static
String
getImg
(
String
content
)
{
if
(
StringUtil
.
isNullOrEmpty
(
content
))
{
return
""
;
}
Pattern
p
=
Pattern
.
compile
(
"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>"
);
// <img[^<>]*src=[\'\"]([0-9A-Za-z.\\/]*)[\'\"].(.*?)>");
Matcher
m
=
p
.
matcher
(
content
);
while
(
m
.
find
())
{
String
img
=
StringUtil
.
getNullStr
(
m
.
group
(
1
));
if
(
StringUtil
.
isNotEmpty
(
img
)
&&
!(
img
.
indexOf
(
"static/kindeditor/plugins/emoticons/images"
)
>
-
1
)
&&
!(
img
.
indexOf
(
"135editor.com/cache"
)
>
-
1
))
{
// 表情包路径,不显示图片
return
img
;
}
// return img;
}
return
""
;
}
//
// public static String getImg(String content) {
// if (StringUtil.isNullOrEmpty(content)) {
// return "";
// }
// Pattern p = Pattern
// .compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");//
// <img[^<>]*src=[\'\"]([0-9A-Za-z.\\/]*)[\'\"].(.*?)>");
// Matcher m = p.matcher(content);
// if (m.find()) {
// String img = StringUtil.getNullStr(m.group(1));
// if (img.indexOf("static/kindeditor/plugins/emoticons/images") > -1) {//
// 表情包路径,不显示图片
// return "";
// }
// // System.out.println("m.group():"+m.group());
// // System.out.println("m.group(1):"+m.group(1));
// return img;
// } else
// return "";
// }
public
static
String
URLDecoder
(
String
s
)
throws
Exception
{
try
{
return
new
String
(
s
.
getBytes
(
"ISO-8859-1"
),
"utf-8"
);
}
catch
(
UnsupportedEncodingException
e
)
{
throw
new
Exception
(
"URL编码失败"
);
}
}
/**
* 格式化字符,去除特殊字符
*
* @param s
* @return
*/
public
static
String
removeSpecialString
(
String
s
)
{
String
result
=
""
;
try
{
if
(
StringUtil
.
isNotEmpty
(
s
))
{
result
=
s
.
replace
(
"-"
,
"031d0e7f454111e790907cd30abc9b82"
);
result
=
s
.
replaceAll
(
"[^(`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?a-zA-Z0-9\\u4e00-\\u9fa5)]"
,
""
);
if
(
StringUtil
.
isNotEmpty
(
result
))
{
result
=
s
.
replaceAll
(
"031d0e7f454111e790907cd30abc9b82"
,
"-"
);
}
}
return
result
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
s
;
}
}
/**
* 格式化字符,只剩下文字、数字
*
* @param s
* @return
*/
public
static
String
FormatString
(
String
s
)
{
String
result
=
""
;
try
{
if
(
StringUtil
.
isNotEmpty
(
s
))
{
result
=
s
.
replaceAll
(
"[^(a-zA-Z0-9)]"
,
""
);
}
return
result
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
s
;
}
}
/**
* 给字符串中的img标签前后添加A标签 A.href = img.src 如果alt为空,则方法不生效 该方法只能适用于img格式为如下形式
* <img src="jpg" alt="" />
*
* @param str
* @param alt
* @return
*/
public
static
String
addAofImg
(
String
str
,
String
alt
)
{
String
result
=
str
;
try
{
if
(
str
.
isEmpty
()
||
alt
.
isEmpty
())
{
return
str
;
}
int
index
=
0
;
/* 方法通过改变alt的值获取下一个img标签 */
while
(
str
.
indexOf
(
"alt=\"\""
)
!=
-
1
)
{
// 获取字符串中图片路径
String
src
=
str
.
substring
(
str
.
indexOf
(
"src=\""
,
str
.
indexOf
(
"<img"
,
index
))
+
5
,
str
.
indexOf
(
"\""
,
str
.
indexOf
(
"src=\""
,
str
.
indexOf
(
"<img"
,
index
))
+
5
));
if
(
str
.
indexOf
(
"alt=\"\" />"
)
!=
-
1
)
{
// 给字符串中img标签前面添加<a>标签
str
=
str
.
substring
(
0
,
str
.
indexOf
(
"<img"
,
index
))
+
"<a href=\""
+
src
+
"\"><img"
+
str
.
substring
(
str
.
indexOf
(
"<img"
,
index
)
+
4
);
index
=
str
.
indexOf
(
"alt=\"\" />"
);
// 添加alt的值,通知在img后面添加</a>标签
str
=
str
.
substring
(
0
,
str
.
indexOf
(
"alt=\"\" />"
))
+
"alt=\""
+
alt
+
"\" />"
+
"</a>"
+
str
.
substring
(
str
.
indexOf
(
"alt=\"\" />"
)
+
9
);
// 截取后再加
}
else
{
// 给字符串中img标签前面添加<a>标签
str
=
str
.
substring
(
0
,
str
.
indexOf
(
"<img"
,
index
))
+
"<a href=\""
+
src
+
"\"><img alt=\""
+
alt
+
"\""
+
str
.
substring
(
str
.
indexOf
(
"<img alt=\"\""
,
index
)
+
11
);
index
=
str
.
indexOf
(
"\""
,
str
.
indexOf
(
"src=\""
,
str
.
indexOf
(
"<img"
,
index
))
+
5
);
// 添加alt的值,通知在img后面添加</a>标签
str
=
str
.
substring
(
0
,
index
+
4
)
+
"</a>"
+
str
.
substring
(
index
+
4
);
// 截取后再加
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
result
;
}
return
str
;
}
/**
* 截取字符串前面的正整数,如"22天"得"22","18个人"得到"18".
*
* @return
*/
public
static
String
getQuantity
(
String
regular
)
{
int
index
=
0
;
for
(
int
i
=
0
;
i
<
regular
.
length
();
i
++)
{
char
c
=
regular
.
charAt
(
i
);
if
(
Character
.
isDigit
(
c
))
{
if
(
i
==
regular
.
length
()
-
1
)
{
index
=
i
+
1
;
}
else
{
index
=
i
;
}
continue
;
}
else
{
index
=
i
;
break
;
}
}
return
regular
.
substring
(
0
,
index
);
}
// public static String getPinYin(String src) {
// char[] t1 = null;
// t1 = src.toCharArray();
// String[] t2 = new String[t1.length];
// // 设置汉字拼音输出的格式
// HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
// t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
// t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
// t3.setVCharType(HanyuPinyinVCharType.WITH_V);
// String t4 = "";
// int t0 = t1.length;
// try {
// for (int i = 0; i < t0; i++) {
// // 判断能否为汉字字符
// // System.out.println(t1[i]);
// if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
// t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
// if (t2 != null) {
// t4 += t2[0];
// }
// } else {
// // 如果不是汉字字符,间接取出字符并连接到字符串t4后
// t4 += Character.toString(t1[i]);
// }
// }
// } catch (BadHanyuPinyinOutputFormatCombination e) {
// e.printStackTrace();
// }
// return t4;
// }
public
static
String
getSubstringStr
(
String
src
,
int
beginIndex
,
int
endIndex
)
{
try
{
return
src
.
substring
(
beginIndex
,
endIndex
);
}
catch
(
Exception
e
)
{
return
""
;
}
}
public
static
String
getSubstringStr
(
String
src
,
int
beginIndex
)
{
try
{
return
src
.
substring
(
beginIndex
);
}
catch
(
Exception
e
)
{
return
""
;
}
}
public
static
String
getRateData
(
String
numerator
,
String
denominator
)
{
String
rate
=
"0%"
;
if
(
StringUtil
.
isNotEmpty
(
denominator
)
&&
!
StringUtil
.
getNullStr
(
denominator
).
equals
(
"0"
)
&&
!
StringUtil
.
getNullStr
(
numerator
).
equals
(
"0"
))
{
return
StringUtil
.
formartDecimal
(
StringUtil
.
getDoubleOfObj
(
numerator
)
/
StringUtil
.
getDoubleOfObj
(
denominator
)
*
100
)
+
"%"
;
}
return
rate
;
}
/**
* 判断字符串数组中是否包含某字符串元素
*
* @param substring
* 某字符串
* @param source
* 源字符串数组
* @return 包含则返回true,否则返回false
*/
public
static
boolean
isIn
(
String
substring
,
String
[]
source
)
{
if
(
source
==
null
||
source
.
length
==
0
)
{
return
false
;
}
for
(
int
i
=
0
;
i
<
source
.
length
;
i
++)
{
String
aSource
=
source
[
i
];
if
(
aSource
.
equals
(
substring
))
{
return
true
;
}
}
return
false
;
}
// public static List<Integer> getAllState() {
// List<Integer> stateList = new ArrayList<Integer>();
// stateList.add(ConstantVar.STATE_OPEN);
// stateList.add(ConstantVar.STATE_STOP);
// return stateList;
// }
/**
* 判断Long类型是否空或者零
*/
public
static
boolean
isNullOrEmpty
(
Long
strLong
)
{
if
(
strLong
==
null
||
strLong
==
0L
||
strLong
.
equals
(
0
))
{
return
true
;
}
return
false
;
}
public
static
String
guid
()
{
Random
rd
=
new
Random
();
char
[]
strRandomList
=
{
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
};
String
result
=
""
;
for
(
int
i
=
0
;
i
<
32
;
i
++)
{
result
+=
strRandomList
[
rd
.
nextInt
(
strRandomList
.
length
)];
// 随机取strRandomList的项值
}
return
result
;
}
/**
* 判断Long类型是否空或者零
*/
public
static
boolean
isNotEmpty
(
Long
strLong
)
{
return
!
isNullOrEmpty
(
strLong
);
}
public
static
Integer
getRandom4Int
()
{
return
r
.
nextInt
(
9000
)
+
1000
;
}
public
static
Integer
getRandom6Int
()
{
return
r
.
nextInt
(
900000
)
+
100000
;
}
public
static
Integer
getRandom9Long
()
{
return
r
.
nextInt
(
900000000
)
+
100000
;
}
public
static
String
getRandomSerialnumber
()
{
String
serialnumber
=
UUID
.
randomUUID
().
toString
();
serialnumber
=
serialnumber
.
replaceAll
(
"-"
,
""
);
return
serialnumber
;
}
/**
* 空字符或null时,返回0
*/
public
static
String
getZeroByNullOrEmpty
(
Object
obj
)
{
String
str
=
StringUtil
.
getNullStr
(
obj
);
if
(
isNullOrEmpty
(
str
))
{
return
"0"
;
}
return
str
;
}
public
static
String
getRandomCharAndNumr
(
Integer
length
)
{
String
str
=
""
;
Random
random
=
new
Random
();
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
boolean
b
=
random
.
nextBoolean
();
if
(
b
)
{
// 字符串
int
choice
=
random
.
nextBoolean
()
?
65
:
97
;
// 取得65大写字母还是97小写字母
str
+=
(
char
)
(
choice
+
random
.
nextInt
(
26
));
// 取得大写字母
}
else
{
// 数字
str
+=
String
.
valueOf
(
random
.
nextInt
(
10
));
}
}
// 如果是纯字母或纯数字 则重新生成
if
(
isRandomUsable
(
str
))
{
str
=
getRandomCharAndNumr
(
length
);
}
return
str
;
}
/**
* 验证随机字母数字组合是否纯数字与纯字母
*/
public
static
boolean
isRandomUsable
(
String
str
)
{
// String regExp =
// "^[A-Za-z]+(([0-9]+[A-Za-z0-9]+)|([A-Za-z0-9]+[0-9]+))|[0-9]+(([A-Za-z]+[A-Za-z0-9]+)|([A-Za-z0-9]+[A-Za-z]+))$";
String
regExp
=
"^([0-9]+)|([A-Za-z]+)$"
;
Pattern
pat
=
Pattern
.
compile
(
regExp
);
Matcher
mat
=
pat
.
matcher
(
str
);
return
mat
.
matches
();
}
public
static
boolean
checkNumValue
(
String
str
)
{
String
reg
=
"^[\\-\\+]?\\d+(\\.\\d+)?"
;
Pattern
pat
=
Pattern
.
compile
(
reg
);
Matcher
mat
=
pat
.
matcher
(
str
);
return
mat
.
matches
();
}
/**
* 去除字符串中的空格、回车、换行符、制表符
*
* @param str
* @return
*/
public
static
String
replaceBlank
(
String
str
)
{
String
dest
=
""
;
if
(
str
!=
null
)
{
Pattern
p
=
Pattern
.
compile
(
"\\s*|\t|\r|\n"
);
Matcher
m
=
p
.
matcher
(
str
);
dest
=
m
.
replaceAll
(
""
);
}
return
dest
;
}
/**
* 判断字符串是否包含一些字符
*/
public
static
boolean
containsString
(
String
src
,
String
dest
)
{
src
=
","
+
getNullStr
(
src
)
+
","
;
dest
=
","
+
getNullStr
(
dest
)
+
","
;
boolean
flag
=
false
;
if
(
src
.
contains
(
dest
))
{
flag
=
true
;
}
return
flag
;
}
public
static
List
<
Long
>
getNullIdList
(
List
<
Object
[]>
objList
,
int
num
)
{
List
<
Long
>
idList
=
new
ArrayList
<
Long
>();
idList
.
add
(
0L
);
if
(
objList
!=
null
&&
objList
.
size
()
>
0
)
{
for
(
Object
[]
obj
:
objList
)
{
idList
.
add
(
getNullLong
(
obj
[
num
]));
}
}
return
idList
;
}
public
static
List
<
String
>
getNullStrIdList
(
List
<
Object
[]>
objList
,
int
num
)
{
List
<
String
>
idList
=
new
ArrayList
<
String
>();
idList
.
add
(
"0"
);
if
(
objList
!=
null
&&
objList
.
size
()
>
0
)
{
for
(
Object
[]
obj
:
objList
)
{
idList
.
add
(
getNullStr
(
obj
[
num
]));
}
}
return
idList
;
}
/**
* JSON字符串特殊字符处理,比如:“\A1;1300”
*
* @param s
* @return String
*/
public
static
String
string2Json
(
String
s
)
{
StringBuffer
sb
=
new
StringBuffer
();
for
(
int
i
=
0
;
i
<
s
.
length
();
i
++)
{
char
c
=
s
.
charAt
(
i
);
switch
(
c
)
{
case
'\"'
:
sb
.
append
(
""
);
break
;
// case '\\':
// sb.append("");
// break;
// case '/':
// sb.append("");
// break;
case
'\b'
:
sb
.
append
(
""
);
break
;
case
'\f'
:
sb
.
append
(
""
);
break
;
case
'\n'
:
sb
.
append
(
""
);
break
;
case
'\r'
:
sb
.
append
(
""
);
break
;
case
'\t'
:
sb
.
append
(
""
);
break
;
default
:
sb
.
append
(
c
);
}
}
return
sb
.
toString
();
}
public
static
String
makeLongListToStr
(
List
<
Long
>
list
)
{
if
(
list
!=
null
&&
!
list
.
isEmpty
())
{
String
str
=
list
.
toString
();
return
str
.
substring
(
1
,
str
.
length
()
-
1
).
replace
(
" "
,
""
);
}
else
{
return
""
;
}
}
public
static
String
makeStringListToStr
(
List
<
String
>
list
)
{
if
(
list
!=
null
&&
!
list
.
isEmpty
())
{
String
str
=
list
.
toString
();
return
str
.
substring
(
1
,
str
.
length
()
-
1
).
replace
(
" "
,
""
);
}
else
{
return
""
;
}
}
/**
* 去除字符串结尾的逗号
*/
public
static
String
removeEndswithComma
(
String
str
)
{
if
(
isNotEmpty
(
str
))
{
if
(
str
.
endsWith
(
","
))
{
str
=
str
.
substring
(
0
,
str
.
length
()
-
1
);
}
}
return
str
;
}
public
static
String
joinArrToStrWithComma
(
String
[]
strArr
)
{
StringBuilder
sb
=
new
StringBuilder
();
if
(
strArr
!=
null
)
{
for
(
String
s
:
strArr
)
{
sb
.
append
(
s
+
","
);
}
}
return
removeEndswithComma
(
sb
.
toString
());
}
/**
* 比较 2个字符串(以','连接的多个字符串元素)是否每个元素都相互包含。。。。
*
* @param str1
* @param str2
* @return
*/
public
static
boolean
compareWhichJoinWithComma
(
String
str1
,
String
str2
)
{
List
<
String
>
list1
=
StringUtil
.
getNullStrToStringList
(
StringUtil
.
getNullStr
(
str1
));
List
<
String
>
list2
=
StringUtil
.
getNullStrToStringList
(
StringUtil
.
getNullStr
(
str2
));
if
(
list1
.
size
()
!=
list2
.
size
())
return
false
;
if
(!
list1
.
containsAll
(
list2
))
return
false
;
if
(!
list2
.
containsAll
(
list1
))
return
false
;
return
true
;
}
/**
* 字符串(以','连接的多个字符串元素)去除重复的子元素
*
* @param str1
* @return
*/
public
static
String
removeRepeatElements
(
String
str
)
{
List
<
String
>
list
=
StringUtil
.
getNullStrToStringList
(
StringUtil
.
getNullStr
(
str
));
List
<
String
>
result
=
new
ArrayList
<
String
>();
for
(
String
s
:
list
)
{
if
(!
result
.
contains
(
s
))
{
result
.
add
(
s
);
}
}
return
makeStringListToStr
(
result
);
}
// 替换所有的回车换行
public
static
String
transferString
(
String
content
)
{
String
string_temp
=
content
;
try
{
if
(
StringUtil
.
isNotEmpty
(
content
))
{
string_temp
=
string_temp
.
replaceAll
(
"<BR>"
,
"\n"
);
}
}
catch
(
Exception
e
)
{
// alert(e.message);
return
string_temp
;
}
return
string_temp
;
}
/**
* 转码 /** 生成随机length 位数字和字母.
*
* @param length
* @return
*/
public
static
String
getStringRandom
(
int
length
)
{
String
val
=
""
;
Random
random
=
new
Random
();
// 参数length,表示生成几位随机数
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
String
charOrNum
=
random
.
nextInt
(
2
)
%
2
==
0
?
"char"
:
"num"
;
// 输出字母还是数字
if
(
"char"
.
equalsIgnoreCase
(
charOrNum
))
{
// 输出是大写字母还是小写字母
int
temp
=
random
.
nextInt
(
2
)
%
2
==
0
?
65
:
97
;
val
+=
(
char
)
(
random
.
nextInt
(
26
)
+
temp
);
}
else
if
(
"num"
.
equalsIgnoreCase
(
charOrNum
))
{
val
+=
String
.
valueOf
(
random
.
nextInt
(
10
));
}
}
if
(
isNumeric
(
val
))
{
val
=
getStringRandom
(
length
);
}
return
val
;
}
public
static
String
unicodeToString
(
String
str
)
{
Pattern
pattern
=
Pattern
.
compile
(
"(\\\\u(\\p{XDigit}{4}))"
);
Matcher
matcher
=
pattern
.
matcher
(
str
);
char
ch
;
while
(
matcher
.
find
())
{
ch
=
(
char
)
Integer
.
parseInt
(
matcher
.
group
(
2
),
16
);
str
=
str
.
replace
(
matcher
.
group
(
1
),
ch
+
""
);
}
return
str
;
}
/**
* 将浮点型保留一位小数
*/
public
static
float
formartOneDecimal
(
Object
d
)
{
if
(
d
==
null
)
{
return
StringUtil
.
getFloatOfObj
(
0
);
}
String
str
=
new
java
.
text
.
DecimalFormat
(
"#.0"
).
format
(
d
);
if
(
str
.
charAt
(
0
)
==
'.'
)
// 0.1 格式化为 .01
{
str
=
"0"
+
str
;
}
if
(
str
.
indexOf
(
"-."
)
==
0
)
// -.01 开头
{
str
=
"-0."
+
str
.
substring
(
1
);
}
if
(
str
.
equals
(
"0"
)
||
str
.
equals
(
"0.0"
))
{
str
=
"0"
;
}
return
StringUtil
.
getFloatOfObj
(
str
.
replace
(
"-0.0"
,
""
).
replace
(
".0"
,
""
));
}
/**
* 判断字符传是全为数字.
*
* @param str
* @return
*/
public
static
boolean
isNumeric
(
String
str
)
{
Pattern
pattern
=
Pattern
.
compile
(
"[0-9]*"
);
Matcher
isNum
=
pattern
.
matcher
(
str
);
if
(!
isNum
.
matches
())
{
return
false
;
}
return
true
;
}
/**
* @param args
*/
public
static
Integer
getIntegerOfObj
(
Object
obj
)
{
try
{
return
Integer
.
valueOf
(
obj
.
toString
());
}
catch
(
Exception
e
)
{
return
Integer
.
valueOf
(
0
);
}
}
/**
* 将Object转为int,如果Object为null返回0
*
* @param args
*/
public
static
int
getNullInt
(
Object
obj
)
{
try
{
return
Integer
.
parseInt
(
obj
.
toString
());
}
catch
(
Exception
e
)
{
return
0
;
}
}
public
static
int
transToIDisplayStart
(
int
pageIndex
,
int
pageSize
)
{
return
(
pageIndex
-
1
)
*
pageSize
;
}
/**
* 入参格式:05:05
*
* @param time
* @return
*/
public
static
int
transTimeToInt
(
String
time
)
{
try
{
String
[]
str
=
time
.
split
(
":"
);
return
Integer
.
valueOf
(
str
[
0
])
*
60
+
Integer
.
valueOf
(
str
[
1
]);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"时间格式错误"
);
}
}
public
static
String
transIntToTime
(
Integer
time
)
{
if
(
time
==
null
)
{
return
""
;
}
try
{
String
minute
=
(
int
)
time
/
60
+
""
;
String
second
=
time
%
60
+
""
;
if
(
minute
.
length
()
==
1
)
{
minute
=
"0"
+
minute
;
}
if
(
second
.
length
()
==
1
)
{
second
=
"0"
+
second
;
}
return
minute
+
":"
+
second
;
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"时间格式错误"
);
}
}
/**
* 替换html符号,存数据库前
*
* @param str
* @return
*/
public
static
String
encodeHtml
(
String
str
)
{
if
(
str
!=
null
)
{
str
=
str
.
replace
(
"'"
,
"''"
);
str
=
str
.
replace
(
"\""
,
"""
);
str
=
str
.
replace
(
"<"
,
"<"
);
str
=
str
.
replace
(
">"
,
">"
);
str
=
str
.
replace
(
"\n"
,
"<br>"
);
str
=
str
.
replace
(
"“"
,
"“"
);
str
=
str
.
replace
(
"”"
,
"”"
);
str
=
str
.
replace
(
"\\"
,
"&bdiag;"
);
}
return
str
;
}
/**
* 还原html符号,从数据库取出后
*
* @param str
* @return
*/
public
static
String
decodeHtml
(
String
str
)
{
if
(
str
!=
null
)
{
str
=
str
.
replace
(
"”"
,
"”"
);
str
=
str
.
replace
(
"“"
,
"“"
);
str
=
str
.
replace
(
">"
,
">"
);
str
=
str
.
replace
(
"<"
,
"<"
);
str
=
str
.
replace
(
"""
,
"\""
);
str
=
str
.
replace
(
"''"
,
"'"
);
str
=
str
.
replace
(
"&bdiag;"
,
"\\"
);
str
=
str
.
replace
(
"'"
,
"'"
);
str
=
str
.
replace
(
"<br>"
,
"\n"
);
}
return
str
;
}
public
static
List
<
Long
>
extractIds
(
List
<
Object
[]>
dataList
)
{
List
<
Long
>
ids
=
new
ArrayList
<
Long
>();
for
(
Object
[]
obj
:
dataList
)
{
Long
id
=
StringUtil
.
getNullLong
(
obj
[
0
]);
ids
.
add
(
id
);
}
return
ids
;
}
public
static
List
<
String
>
extractIdsToStrList
(
List
<
Object
[]>
dataList
)
{
List
<
String
>
ids
=
new
ArrayList
<
String
>();
for
(
Object
[]
obj
:
dataList
)
{
String
id
=
StringUtil
.
getNullStr
(
obj
[
0
]);
ids
.
add
(
id
);
}
return
ids
;
}
/**
* 提供精确的加法运算。
*
* @param v1
* 被加数
* @param v2
* 加数
* @return 两个参数的和
*/
public
static
double
add
(
double
v1
,
double
v2
)
{
BigDecimal
b1
=
new
BigDecimal
(
Double
.
toString
(
v1
));
BigDecimal
b2
=
new
BigDecimal
(
Double
.
toString
(
v2
));
return
b1
.
add
(
b2
).
doubleValue
();
}
/**
* 提供精确的减法运算。
*
* @param v1
* 被减数
* @param v2
* 减数
* @return 两个参数的差
*/
public
static
double
sub
(
double
v1
,
double
v2
)
{
BigDecimal
b1
=
new
BigDecimal
(
Double
.
toString
(
v1
));
BigDecimal
b2
=
new
BigDecimal
(
Double
.
toString
(
v2
));
return
b1
.
subtract
(
b2
).
doubleValue
();
}
/**
* 提供精确的乘法运算。
*
* @param v1
* 被乘数
* @param v2
* 乘数
* @return 两个参数的积
*/
public
static
double
mul
(
double
v1
,
double
v2
)
{
BigDecimal
b1
=
new
BigDecimal
(
Double
.
toString
(
v1
));
BigDecimal
b2
=
new
BigDecimal
(
Double
.
toString
(
v2
));
return
b1
.
multiply
(
b2
).
doubleValue
();
}
/**
* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到 小数点以后10位,以后的数字四舍五入。
*
* @param v1
* 被除数
* @param v2
* 除数
* @return 两个参数的商
*/
public
static
double
div
(
double
v1
,
double
v2
)
{
return
div
(
v1
,
v2
,
10
);
}
public
static
double
divPercent
(
double
v1
,
double
v2
,
int
scale
)
{
return
mul
(
div
(
v1
,
v2
,
scale
+
2
),
100
);
}
/**
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。
*
* @param v1
* 被除数
* @param v2
* 除数
* @param scale
* 表示表示需要精确到小数点以后几位。
* @return 两个参数的商
*/
public
static
double
div
(
double
v1
,
double
v2
,
int
scale
)
{
if
(
scale
<
0
)
{
throw
new
IllegalArgumentException
(
"The scale must be a positive integer or zero"
);
}
BigDecimal
b1
=
new
BigDecimal
(
Double
.
toString
(
v1
));
BigDecimal
b2
=
new
BigDecimal
(
Double
.
toString
(
v2
));
return
b1
.
divide
(
b2
,
scale
,
BigDecimal
.
ROUND_HALF_UP
).
doubleValue
();
}
/**
* 提供精确的小数位四舍五入处理。
*
* @param v
* 需要四舍五入的数字
* @param scale
* 小数点后保留几位
* @return 四舍五入后的结果
*/
public
static
double
round
(
double
v
,
int
scale
)
{
if
(
scale
<
0
)
{
throw
new
IllegalArgumentException
(
"The scale must be a positive integer or zero"
);
}
BigDecimal
b
=
new
BigDecimal
(
Double
.
toString
(
v
));
BigDecimal
one
=
new
BigDecimal
(
"1"
);
return
b
.
divide
(
one
,
scale
,
BigDecimal
.
ROUND_HALF_UP
).
doubleValue
();
}
/**
* true:合法 <br>
* false:不合法
*
* @param phonenum
* @return
*/
public
static
boolean
checkPhone
(
String
phonenum
)
{
return
StringUtil
.
isNullOrEmpty
(
phonenum
)
||
phonenum
.
matches
(
"^[0-9-]{7,13}$"
);
}
/**
* 校验时间格式
*
* @param date
* @return
*/
public
static
boolean
checkDate
(
String
date
)
{
boolean
dateType
=
false
;
try
{
String
rexp
=
"^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))"
;
Pattern
pat
=
Pattern
.
compile
(
rexp
);
Matcher
mat
=
pat
.
matcher
(
date
);
dateType
=
mat
.
matches
();
}
catch
(
Exception
e
)
{
return
false
;
}
return
dateType
;
}
/**
* 判断数字串是否为空 或者零
*/
public
static
boolean
isNullOrEmpty
(
Integer
num
)
{
if
(
num
==
null
||
num
==
0
)
{
return
true
;
}
return
false
;
}
/**
* 判断数字串是否为空 或者零
*/
public
static
boolean
isNotEmpty
(
Integer
num
)
{
return
!
isNullOrEmpty
(
num
);
}
public
static
boolean
getBooleanByOperationStr
(
String
str
)
{
ScriptEngineManager
manager
=
new
ScriptEngineManager
();
ScriptEngine
engine
=
manager
.
getEngineByName
(
"javascript"
);
boolean
result
=
false
;
try
{
result
=
StringUtil
.
getBooleanOfObj
(
engine
.
eval
(
str
));
}
catch
(
ScriptException
e
)
{
e
.
printStackTrace
();
}
return
result
;
}
public
static
String
createTimeString
()
{
Date
date
=
new
Date
();
SimpleDateFormat
df
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
return
df
.
format
(
date
);
}
public
static
Boolean
getExpressionResult
(
String
expression
)
{
Boolean
result
=
false
;
try
{
ScriptEngineManager
manager
=
new
ScriptEngineManager
();
ScriptEngine
engine
=
manager
.
getEngineByName
(
"js"
);
result
=
(
Boolean
)
engine
.
eval
(
expression
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"转换运算表达式有误;error:"
+
e
.
getMessage
()
+
"expression="
+
expression
);
result
=
false
;
}
return
result
;
}
/**
* 自动将字符串补足N位 ,如1111——》0000001111
*
* @param kh
* @return
*/
public
static
String
fixToNum
(
String
kh
,
int
num
)
{
if
(
StringUtil
.
isNotEmpty
(
kh
)
&&
kh
.
length
()
<
num
)
{
for
(
int
i
=
num
-
kh
.
length
();
i
>
0
;
i
--)
{
kh
=
"0"
+
kh
;
}
}
return
kh
;
}
public
static
String
getChineseNumber
(
Integer
number
)
{
return
CHINESE_NUMBER
[
number
-
1
];
}
/**
* 0和负数视为null
*
* @param kh
* @return
*/
public
static
Float
getNullFloat
(
Float
value
)
{
if
(
value
!=
null
&&
value
>
0
f
)
{
return
value
;
}
else
{
return
null
;
}
}
/**
* 获取两位小数
*
* @param kh
* @return
*/
public
static
String
formartFloat
(
Float
value
)
{
if
(
value
!=
null
)
{
return
decimalFormat
.
format
(
value
);
}
else
{
return
null
;
}
}
public
static
String
formart4DecimalFloat
(
Float
value
)
{
if
(
value
!=
null
)
{
String
str
=
DECIMAL_FORMAT_FOUR
.
format
(
value
);
if
(
str
.
length
()
>
0
)
{
if
(
str
.
substring
(
str
.
length
()
-
3
,
str
.
length
()).
equals
(
"000"
))
{
str
=
str
.
substring
(
0
,
str
.
length
()
-
3
);
}
else
if
(
str
.
substring
(
str
.
length
()
-
2
,
str
.
length
()).
equals
(
"00"
))
{
str
=
str
.
substring
(
0
,
str
.
length
()
-
2
);
}
else
if
(
str
.
substring
(
str
.
length
()
-
1
,
str
.
length
()).
equals
(
"0"
))
{
str
=
str
.
substring
(
0
,
str
.
length
()
-
1
);
}
}
return
str
.
replace
(
"0.0000"
,
"0"
);
}
else
{
return
null
;
}
}
// 根据经纬度计算两个点之间的直线距离(单位:米)
public
static
double
getDistance
(
double
lat1
,
double
lng1
,
double
lat2
,
double
lng2
)
{
double
a
=
rad
(
lat1
)
-
rad
(
lat2
);
double
b
=
rad
(
lng1
)
-
rad
(
lng2
);
double
s
=
2
*
Math
.
asin
(
Math
.
sqrt
(
Math
.
pow
(
Math
.
sin
(
a
/
2
),
2
)
+
Math
.
cos
(
rad
(
lat1
))
*
Math
.
cos
(
rad
(
lat2
))
*
Math
.
pow
(
Math
.
sin
(
b
/
2
),
2
)));
s
=
s
*
EARTH_RADIUS
;
s
=
Math
.
round
(
s
*
10000
d
)
/
10000
d
;
s
=
s
*
1000
;
return
s
;
}
private
static
double
rad
(
double
d
)
{
return
d
*
Math
.
PI
/
180.0
;
}
public
static
String
makeDistanceStr
(
Double
distance
)
{
if
(
distance
>=
1000
D
)
{
return
StringUtil
.
formartDecimal
(
distance
/
1000
)
+
"km"
;
}
return
StringUtil
.
formartDecimal
(
distance
)
+
"m"
;
}
/**
* 两个数字相乘 d1*d2
*
* @param d1
* @param d2
* @return
*/
public
static
double
mul
(
String
d1
,
String
d2
)
{
try
{
BigDecimal
b1
=
new
BigDecimal
(
d1
);
BigDecimal
b2
=
new
BigDecimal
(
d2
);
return
b1
.
multiply
(
b2
).
doubleValue
();
}
catch
(
Exception
e
)
{
return
0
;
}
}
/**
* 获取一定长度的随机字符串
*
* @param length
* 指定字符串长度
* @return 一定长度的字符串
*/
public
static
String
getRandomStringByLength
(
int
length
)
{
String
base
=
"abcdefghijklmnopqrstuvwxyz0123456789"
;
Random
random
=
new
Random
();
StringBuffer
sb
=
new
StringBuffer
();
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
int
number
=
random
.
nextInt
(
base
.
length
());
sb
.
append
(
base
.
charAt
(
number
));
}
return
sb
.
toString
();
}
/**
* 校验经纬度坐标格式
*
* @param coordinate
* @return
*/
public
static
boolean
isRegex
(
String
coordinate
)
{
String
regex
=
"^((-)?\\d*(\\.)?\\d*)[,]((-)?\\d*(\\.)?\\d*)$"
;
boolean
result
=
Pattern
.
compile
(
regex
).
matcher
(
coordinate
).
find
();
if
(!
result
)
{
throw
new
RuntimeException
(
"经纬度坐标不正确:coordinate:"
+
coordinate
);
}
return
result
;
}
public
static
String
geImgtHttpUrl
(
String
imgUrl
)
{
if
(
StringUtil
.
isNullOrEmpty
(
imgUrl
))
{
return
""
;
}
if
(
imgUrl
.
length
()
>
500
)
{
return
""
;
}
if
(
imgUrl
.
indexOf
(
"http://"
)
>
-
1
||
imgUrl
.
indexOf
(
"https://"
)
>
-
1
)
{
return
imgUrl
;
}
return
""
;
}
public
static
String
length50
(
String
content
)
{
String
result
=
content
;
if
(
StringUtil
.
isNullOrEmpty
(
content
))
{
return
""
;
}
if
(
content
.
length
()
>=
60
)
{
result
=
content
.
substring
(
0
,
49
)
+
"..."
;
}
return
result
;
}
public
static
String
length1000
(
String
content
)
{
String
result
=
content
;
if
(
StringUtil
.
isNullOrEmpty
(
content
))
{
return
""
;
}
if
(
content
.
length
()
>=
1000
)
{
result
=
content
.
substring
(
0
,
500
)
+
"..."
;
}
return
result
;
}
public
static
String
clearInvisibleCharacter
(
String
content
)
{
if
(
isNotEmpty
(
content
))
{
char
[]
contentCharArr
=
content
.
toCharArray
();
for
(
int
i
=
0
;
i
<
contentCharArr
.
length
;
i
++)
{
if
(
contentCharArr
[
i
]
<
0x20
||
contentCharArr
[
i
]
==
0x7F
)
{
contentCharArr
[
i
]
=
0x20
;
}
}
return
new
String
(
contentCharArr
);
}
return
""
;
}
public
static
String
removeHtml
(
String
content
)
{
String
regExHtml
=
"<[^>]+>"
;
// 定义HTML标签的正则表达式
Pattern
pHtml
=
Pattern
.
compile
(
regExHtml
,
Pattern
.
CASE_INSENSITIVE
);
Matcher
mHtml
=
pHtml
.
matcher
(
content
);
content
=
mHtml
.
replaceAll
(
""
);
return
content
;
}
/**
* 判断身份证格式
*
* @param idNum
* @return
*/
public
static
Map
<
String
,
Object
>
getIdNumberInfo
(
String
idNum
)
{
Map
<
String
,
Object
>
idNumberMap
=
new
HashMap
<
String
,
Object
>();
boolean
isCorrect
=
true
;
// 中国公民身份证格式:长度为15或18位,最后一位可以为字母
Pattern
idNumPattern
=
Pattern
.
compile
(
"(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])"
);
// 格式验证
if
(!
idNumPattern
.
matcher
(
idNum
).
matches
())
isCorrect
=
false
;
// 合法性验证
int
year
=
0
;
int
month
=
0
;
int
day
=
0
;
if
(
idNum
.
length
()
==
15
)
{
// 一代身份证
System
.
out
.
println
(
"一代身份证:"
+
idNum
);
// 提取身份证上的前6位以及出生年月日
Pattern
birthDatePattern
=
Pattern
.
compile
(
"\\d{6}(\\d{2})(\\d{2})(\\d{2}).*"
);
Matcher
birthDateMather
=
birthDatePattern
.
matcher
(
idNum
);
if
(
birthDateMather
.
find
())
{
year
=
Integer
.
valueOf
(
"19"
+
birthDateMather
.
group
(
1
));
month
=
Integer
.
valueOf
(
birthDateMather
.
group
(
2
));
day
=
Integer
.
valueOf
(
birthDateMather
.
group
(
3
));
}
}
else
if
(
idNum
.
length
()
==
18
)
{
// 二代身份证
System
.
out
.
println
(
"二代身份证:"
+
idNum
);
// 提取身份证上的前6位以及出生年月日
Pattern
birthDatePattern
=
Pattern
.
compile
(
"\\d{6}(\\d{4})(\\d{2})(\\d{2}).*"
);
Matcher
birthDateMather
=
birthDatePattern
.
matcher
(
idNum
);
if
(
birthDateMather
.
find
())
{
year
=
Integer
.
valueOf
(
birthDateMather
.
group
(
1
));
month
=
Integer
.
valueOf
(
birthDateMather
.
group
(
2
));
day
=
Integer
.
valueOf
(
birthDateMather
.
group
(
3
));
}
}
// 年份判断,100年前至今
Calendar
cal
=
Calendar
.
getInstance
();
// 当前年份
int
currentYear
=
cal
.
get
(
Calendar
.
YEAR
);
if
(
year
<=
currentYear
-
100
||
year
>
currentYear
)
isCorrect
=
false
;
// 月份判断
if
(
month
<
1
||
month
>
12
)
isCorrect
=
false
;
// 日期判断
// 计算月份天数
int
dayCount
=
31
;
switch
(
month
)
{
case
1
:
case
3
:
case
5
:
case
7
:
case
8
:
case
10
:
case
12
:
dayCount
=
31
;
break
;
case
2
:
// 2月份判断是否为闰年
if
((
year
%
4
==
0
&&
year
%
100
!=
0
)
||
(
year
%
400
==
0
))
{
dayCount
=
29
;
break
;
}
else
{
dayCount
=
28
;
break
;
}
case
4
:
case
6
:
case
9
:
case
11
:
dayCount
=
30
;
break
;
}
if
(
day
<
1
||
day
>
dayCount
)
isCorrect
=
false
;
try
{
if
(
idNum
.
length
()
==
15
)
{
idNumberMap
=
getCarInfo15W
(
idNum
);
}
else
if
(
idNum
.
length
()
==
18
)
{
idNumberMap
=
getCarInfo
(
idNum
);
}
}
catch
(
Exception
e
)
{
isCorrect
=
false
;
}
idNumberMap
.
put
(
"isCorrect"
,
isCorrect
);
return
idNumberMap
;
}
/**
* 15位身份证的验证
*
* @param
* @throws Exception
*/
public
static
Map
<
String
,
Object
>
getCarInfo15W
(
String
card
)
throws
Exception
{
Map
<
String
,
Object
>
map
=
new
HashMap
<
String
,
Object
>();
String
fyear
=
"19"
+
card
.
substring
(
6
,
8
);
// 年份
String
fyue
=
card
.
substring
(
8
,
10
);
// 月份
String
fday
=
card
.
substring
(
10
,
12
);
// 日
String
usex
=
card
.
substring
(
14
,
15
);
// 用户的性别
String
sex
;
if
(
Integer
.
parseInt
(
usex
)
%
2
==
0
)
{
sex
=
"2"
;
}
else
{
sex
=
"1"
;
}
Date
date
=
new
Date
();
// 得到当前的系统时间
SimpleDateFormat
format
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
String
year
=
format
.
format
(
date
).
substring
(
0
,
4
);
// 当前年份
String
yue
=
format
.
format
(
date
).
substring
(
5
,
7
);
// 月份
// String fday = format.format(date).substring(8, 10);
int
age
=
0
;
if
(
Integer
.
parseInt
(
yue
)
<=
Integer
.
parseInt
(
fyue
))
{
//
age
=
Integer
.
parseInt
(
year
)
-
Integer
.
parseInt
(
fyear
)
+
1
;
}
else
{
// 当前用户还没过生
age
=
Integer
.
parseInt
(
year
)
-
Integer
.
parseInt
(
fyear
);
}
map
.
put
(
"age"
,
age
);
map
.
put
(
"sex"
,
sex
);
map
.
put
(
"birthday"
,
fyear
+
"-"
+
fyue
+
"-"
+
fday
);
return
map
;
}
/**
* 根据身份证的号码算出当前身份证持有者的性别和年龄 18位身份证
*
* @return
* @throws Exception
*/
public
static
Map
<
String
,
Object
>
getCarInfo
(
String
CardCode
)
throws
Exception
{
Map
<
String
,
Object
>
map
=
new
HashMap
<
String
,
Object
>();
String
fyear
=
CardCode
.
substring
(
6
).
substring
(
0
,
4
);
// 得到年份
String
fyue
=
CardCode
.
substring
(
10
).
substring
(
0
,
2
);
// 得到月份
String
fday
=
CardCode
.
substring
(
12
).
substring
(
0
,
2
);
// 得到日
String
sex
;
if
(
Integer
.
parseInt
(
CardCode
.
substring
(
16
).
substring
(
0
,
1
))
%
2
==
0
)
{
// 判断性别
sex
=
"2"
;
}
else
{
sex
=
"1"
;
}
Date
date
=
new
Date
();
// 得到当前的系统时间
SimpleDateFormat
format
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
String
year
=
format
.
format
(
date
).
substring
(
0
,
4
);
// 当前年份
String
yue
=
format
.
format
(
date
).
substring
(
5
,
7
);
// 月份
// String day = format.format(date).substring(8, 10);
int
age
=
0
;
if
(
Integer
.
parseInt
(
yue
)
<=
Integer
.
parseInt
(
fyue
))
{
//
age
=
Integer
.
parseInt
(
year
)
-
Integer
.
parseInt
(
fyear
)
+
1
;
}
else
{
// 当前用户还没过生
age
=
Integer
.
parseInt
(
year
)
-
Integer
.
parseInt
(
fyear
);
}
map
.
put
(
"age"
,
age
);
map
.
put
(
"sex"
,
sex
);
map
.
put
(
"birthday"
,
fyear
+
"-"
+
fyue
+
"-"
+
fday
);
return
map
;
}
/**
* 取出html代码中的网络图片
*
* @author cfq
* @param content
* @return
*/
public
static
List
<
String
>
getStringImgs
(
String
content
)
{
List
<
String
>
result
=
new
ArrayList
<>();
if
(
StringUtil
.
isNullOrEmpty
(
content
))
{
return
result
;
}
Pattern
p
=
Pattern
.
compile
(
"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>"
);
Matcher
m
=
p
.
matcher
(
content
);
while
(
m
.
find
())
{
String
img
=
StringUtil
.
getNullStr
(
m
.
group
(
1
));
if
(
StringUtil
.
isNotEmpty
(
img
))
{
result
.
add
(
img
);
}
}
return
result
;
}
/**
* 替换字符串内容
*
* @author cfq
* @param content
* @param ordStr
* @param newStr
* @return
*/
public
static
String
replace
(
String
content
,
String
ordStr
,
String
newStr
)
{
if
(
StringUtil
.
isNullOrEmpty
(
content
))
{
return
""
;
}
return
content
.
replace
(
ordStr
,
newStr
);
}
public
static
Integer
formartBigInteger
(
BigInteger
bigInteger
)
{
return
bigInteger
==
null
?
0
:
bigInteger
.
intValue
();
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment