/*
* TextProcessor.java
*
*/
package com.aotain.project.util;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import sun.misc.BASE64Decoder;
/**
* A TextProcessor is a convenient class for handling some common text
* processing.
*/
public class StringUtil {
public final static String EMPTY_STR = "";
public static String EN = "en";
public static String CH = "ch";
public static String SQL_ESCAPE_CHAR = "/";
/** Creates new TextProcessor */
private StringUtil() {
}
public static byte[] hexToBytes(String hexStr) throws Exception {
if (hexStr == null) {
return null;
}
if (hexStr.length() % 2 != 0) {
throw new Exception("Length of data is not equal to even number");
}
byte[] rtnBytes = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
rtnBytes[i] = (byte) Integer.parseInt(hexStr.substring(i * 2,
i * 2 + 2), 16);
}
return rtnBytes;
}
/**
* Convert a long value in millisec to hh:MM:ss
*/
public static String longToHHMMSS(long someTime) {
long totalSec = someTime / 1000;
long sec = totalSec % 60;
String ss = leftPad(Long.toString(sec), 2, '0');
long min = ((totalSec - sec) / 60) % 60;
String mm = leftPad(Long.toString(min), 2, '0');
String hh = Long.toString((totalSec - sec) / 3600);
return new StringBuffer().append(hh).append(':').append(mm).append(':')
.append(ss).toString();
}
public static String leftPad(String inStr, int length, char c) {
if (inStr.length() == length) {
return inStr;
}
StringBuffer outStr = new StringBuffer();
for (int i = inStr.length(); i < length; i++) {
outStr.append(c);
}
outStr.append(inStr);
return outStr.toString();
}
public static String rightPad(String inStr, int length, char c) {
if (inStr.length() == length) {
return inStr;
}
StringBuffer outStr = new StringBuffer();
outStr.append(inStr);
for (int i = inStr.length(); i < length; i++) {
outStr.append(c);
}
return outStr.toString();
}
/**
* Tokenizes a given string according to the specified delimiters. The
* characters in the delim argument are the delimiters for separating
* tokens. Delimiter characters themselves will not be treated as tokens.
*
* @param str
* A string to be parsed.
* @param delim
* The delimiters.
* @return The tokens in a String array.
*/
public static String[] tokenize(String str, String delim) {
String[] strs = null;
if (str != null) {
StringTokenizer tokens;
if (delim == null) {
tokens = new StringTokenizer(str);
} else {
tokens = new StringTokenizer(str, delim);
}
strs = new String[tokens.countTokens()];
for (int i = 0; i < strs.length && tokens.hasMoreTokens(); i++) {
strs[i] = tokens.nextToken();
}
}
return strs;
}
/**
* Tokenizes a given string according to a fixed length. If the last token's
* length is less than the fixed length specified, it will be ignored.
*
* @param str
* A string to be parsed.
* @param fixedLength
* The fixed length.
* @return The tokens in a String array.
*/
public static String[] tokenize(String str, int fixedLength) {
String[] strs = null;
if (str != null && fixedLength > 0) {
Vector<String> v = new Vector<String>();
for (int i = 0; i < str.length(); i += fixedLength) {
int next = i + fixedLength;
if (next > str.length()) {
next = str.length();
}
v.addElement(str.substring(i, next));
}
strs = v.toArray(new String[] {});
}
return strs;
}
/**
* Concatenates a String array (String tokens) into a String with the
* specified delimiter String.
*
* @param tokens
* A String array to be concatenated.
* @param delim
* The delimiter.
* @return The concatenated String.
*/
public static String concat(String[] tokens, String delim) {
return concat(tokens, EMPTY_STR, EMPTY_STR, delim);
}
/**
* Concatenates a String array (String tokens) into a String with the
* specified delimiter String, token's prefix, and token's suffix.
*
* @param tokens
* A String array to be concatenated.
* @param tokenPrefix
* The token's prefix to be concatenated.
* @param tokenSuffix
* The token's suffix to be concatenated.
* @param delim
* The delimiter.
* @return The concatenated String.
*/
public static String concat(String[] tokens, String tokenPrefix,
String tokenSuffix, String delim) {
StringBuffer s = new StringBuffer();
if (tokens != null) {
if (tokenPrefix == null) {
tokenPrefix = EMPTY_STR;
}
if (tokenSuffix == null) {
tokenSuffix = EMPTY_STR;
}
if (delim == null) {
delim = EMPTY_STR;
}
for (int i = 0; i < tokens.length; i++) {
// s += tokenPrefix+tokens[i]+tokenSuffix;
// if (i+1<tokens.length) s += delim;
s.append(tokenPrefix).append(tokens[i]).append(tokenSuffix);
if (i + 1 < tokens.length) {
s.append(delim);
}
}
}
return s.toString();
}
public static String concatIgnoreNull(String str1, String str2) {
if (null != str1 && null != str2) {
return str1.concat(str2);
}
if (null != str1) {
return str1;
}
if (null != str2) {
return str2;
}
return "";
}
/**
* Checks if a given String array contains the specified search String.
*
* @param tokens
* A String array to be searched.
* @param target
* The target search String.
* @return <PRE>
* true
* </PRE>
*
* if the given String array contains the specified search String,
*
* <PRE>
* false
* </PRE>
*
* otherwise.
*/
public static boolean contains(String[] tokens, String target) {
if (tokens != null) {
for (int i = 0; i < tokens.length; i++) {
if (tokens[i] == null) {
if (target == null) {
return true;
}
} else {
if (tokens[i].equals(target)) {
return true;
}
}
}
}
return false;
}
/**
* Repeats a given String in the specified number of times, then
* concatenates and returns it.
*
* @param s
* A String to be repeated and concatenated.
* @param occurs
* The number of times of the given String to be repeated.
* @return The concatenated String.
*/
public static String repeatString(String s, int occurs) {
StringBuffer result = new StringBuffer();
if (s != null && occurs > 0) {
for (int i = 0; i < occurs; i++) {
result.append(s);
}
}
return result.toString();
}
/**
* Checks if a given String contains only digits.
*
* @param s
* A String to be checked.
* @return <PRE>
* true
* </PRE>
*
* if the given String contains only digits,
*
* <PRE>
* false
* </PRE>
*
* otherwise.
*/
public static boolean isAllDigit(String s) {
if (s == null || s.equals(EMPTY_STR)) {
return false;
} else {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}
}
public static boolean isAllAlphanumeric(String s) {
if (s == null || s.equals(EMPTY_STR)) {
return
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
project.zip (77个子文件)
project
src
main
resources
java
com
aotain
project
dto
ResultEntity.java 539B
dao
UserMapper.java 130B
controller
ViewInterceptor.java 889B
ExceptionHandlerController.java 1KB
util
ToolsUtil.java 4KB
LogUtil.java 3KB
ParamUtils.java 1KB
TextUtil.java 465B
RandomNumber.java 653B
Tools.java 7KB
Param.java 807B
DateFormatConstant.java 2KB
ReadUserXml.java 2KB
PushParamsDecoder.java 2KB
DateUtils.java 20KB
Constants.java 764B
StringUtil.java 34KB
bean
service
UserService.java 79B
impl
UserServiceImpl.java 390B
mapping
UserMapper.xml 796B
datasource
RoutingDataSource.java 310B
DataSourceContextHolder.java 401B
conf
spring-mybatis.xml 4KB
jdbc.properties 125B
spring-mvc.xml 1KB
spring.xml 922B
log4j.properties 2KB
common.properties 0B
webapp
resources
style
js
util
util.js 2KB
jquery-1.12.4.min.js 95KB
WEB-INF
views
error
500.jsp 1KB
404.jsp 1KB
common
taglibs.jsp 1KB
web.xml 2KB
test
java
.classpath 1KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.m2e.core.prefs 90B
org.eclipse.core.resources.prefs 155B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 662B
.jsdtscope 575B
org.eclipse.jdt.core.prefs 430B
org.eclipse.wst.validation.prefs 50B
pom.xml 7KB
target
classes
com
aotain
project
dto
ResultEntity.class 1005B
dao
UserMapper.class 124B
controller
ViewInterceptor.class 1KB
ExceptionHandlerController.class 2KB
util
StringUtil.class 19KB
DateUtils.class 16KB
ParamUtils.class 2KB
ReadUserXml.class 306B
RandomNumber.class 979B
Param.class 1KB
LogUtil.class 3KB
DateFormatConstant.class 2KB
Tools.class 7KB
ToolsUtil.class 5KB
Constants.class 1KB
TextUtil.class 806B
PushParamsDecoder.class 2KB
bean
service
UserService.class 130B
impl
UserServiceImpl.class 617B
mapping
UserMapper.xml 796B
datasource
RoutingDataSource.class 603B
DataSourceContextHolder.class 996B
conf
spring-mybatis.xml 4KB
jdbc.properties 163B
spring-mvc.xml 1KB
spring.xml 922B
log4j.properties 2KB
common.properties 0B
test-classes
m2e-wtp
web-resources
META-INF
MANIFEST.MF 114B
maven
aotain
project
pom.properties 227B
pom.xml 7KB
.project 1KB
共 77 条
- 1
资源评论
- cai1632020-05-08初学者,希望少走坑的代码,谢谢
- 12点一刻2018-03-13垃圾的代码,不能用fly小胖2020-04-22出了啥问题
fly小胖
- 粉丝: 26
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功