package com.yj.project.utils;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字符串处理工具类 <BR>
*
* <B>修改记录: </B> <BR>
*
* <PRE>
*
* 日期 修改人 修改内容
* 2007/12/26 周岩 新建
*
* 日期 修改人 修改内容
* 2008/08/05 代冉冉 增加注释
*
* </PRE>
*
* @author netstars
*/
public class StringUtil {
/**
* 将字符串第一个字母转为大写
* @param str
* @return
*/
public static String convertFirstToUpper(String str){
String str1 = str.substring(0,1).toUpperCase();
String str2 = str.substring(1,str.length());
return str1+str2;
}
/**
* 获取字符串的行数
* @param str
* @return
*/
public static int getLines(String str,String patern){
if(str!=null && !"".equals(str)){
return str.split(patern).length;
}else{
return 0;
}
}
/**
* 获取字符串的行数
* @param str
* @return
*/
public static String[] getArrStr(String str,String patern){
if(str!=null && !"".equals(str)){
return str.split(patern);
}else{
return null;
}
}
/**
* replaceSpecialChar:替换掉特殊字符
* @param strFileContent
* @return
*/
public static String replaceSpecialChar(String strFileContent){
Pattern pattern = Pattern.compile("[<>*?|\\/:\"]+");
Matcher m = pattern.matcher(strFileContent);
strFileContent = m.replaceAll("");
return strFileContent;
}
/**
* null判断,如果为null,返回给定的字串str2
* @param strFileContent
* @param str 原字符串
* @param str2 给定的字符
* @return
*/
public static String nullString(String str,String str2){
str = (str==null)?str2:str;
str = (str.equals("null"))?str2:str;
return str;
}
/**
* 空判断,如果为空,返回给定的字串str2
* @param strFileContent
* @param str 原字符串
* @param str2 给定的字符
* @return
*/
public static String emptyString(String str,String str2){
str = nullString(str,str2);
str = (str.equals(""))?str2:str;
return str;
}
/**
* null判断,如果为null,返回给定的字串str2
* @param strFileContent
* @param str 原字符串
* @param str2 给定的字符
* @return
*/
public static int nullInt(Integer in,int in2){
in = (in==null)?in2:in;
return in;
}
/**
* null判断,如果为null,返回给定的字串int2
* @param in 原字符串
* @param int2 给定的字符
* @return
*/
public static double nullDouble(Double dbl,double dbl2){
dbl = (dbl==null)?dbl2:dbl;
return dbl;
}
/**
* null判断,如果为null,返回给定的数值fl2
* @param fl 原数值
* @param fl2 给定的数值
* @return
*/
public static float nullFloat(Float fl, float fl2) {
fl = (fl == null) ? fl2 : fl;
return fl;
}
/**
* 将字符串中的行结束符、分隔符和转义字符按照给定的转义符进行转义处理,如果字串为null,则返回给定的字符串
*
* @param str
* 原字符串
* @param str2
* 给定的字符
* @param rowendchar:行结束符
* @param splitStr:分隔符
* @param transStr:转义字符
* @return
*/
public static String transString(String str,String str2,String rowendchar,String splitStr,String transStr){
str = nullString(str, str2);// null处理
str = StringUtil.replaceRN(str);
if (!str.equals(str2)) {
str = str.replaceAll(transStr, transStr + transStr);// 转义转义符
str = str.replaceAll(splitStr, transStr + splitStr);// 转义分隔符
str = str.replaceAll(rowendchar, transStr + rowendchar);// 转义行结束符
}
return str;
}
/**
* getStringByLocation: 根据指定行的位置获取信息
*
* @param strFile
* 给定字串,从1开始计算的
* @param iStart
* 起始位置,
* @param iLen
* 截取的字串长度
* @return
*/
public static String getStringByLocation(String strFile, int iStart,int iLen) {
String strLocate = "";
try {
if (strFile.length() >= (iStart + iLen)) {
strLocate = strFile.substring(iStart - 1, iStart + iLen - 1);
} else if (strFile.length() >= iStart) {
strLocate = strFile.substring(iStart - 1);
} else {
strLocate = "";
}
} catch (Exception e) {
System.out.println(e);
}
return strLocate.trim();// strFile2
}
/**
* getStringByLocation: 得到指定字符之前的字串
* @param strFile 给定字串
* @param loc 指定字符,默认为一个空格
* @return
*/
public static String getStringByLocation(String strFile, String loc) {
if ("".equals(loc)) {
loc = " ";
}
// add begin by zy 2008.2.20 如果不存在指定的字符串loc
// strFile = strFile.substring(0,strFile.indexOf(loc));这一句会报错
if (strFile.indexOf(loc) == -1)
return strFile;
// add end by zy 2008.2.20
try {
if (!"".equals(strFile)) {
strFile = strFile.trim();
strFile = strFile.substring(0, strFile.indexOf(loc));
} else {
return "";
}
} catch (Exception e) {
System.out.println(e);
}
return strFile;// strFile2
}
/**
* getStringByLocation: 根据指定行的位置获取信息
* @param strFile 给定字串,从1开始计算的
* @param iStart 起始位置,
* @param iLen 截取的字串长度
* @param strAdd 后面增加指定字符
* @return
*/
public static String getStringByLocation(String strFile, int iStart,int iLen,String strAdd) {
String strLocate = "";
try {
if (strFile.length() >= (iStart + iLen)) {
strLocate = strFile.substring(iStart - 1, iStart + iLen - 1);
} else if (strFile.length() >= iStart) {
strLocate = strFile.substring(iStart - 1);
} else {
strLocate = "";
}
} catch (Exception e) {
System.out.println(e);
}
return strLocate.trim()+strAdd;// strFile2
}
/**
* getStringByLocation: 根据指定行的位置获取信息
* @param strFile 给定字串
* @param iStart 起始位置,
* @param iEnd 结束位置
* @return
*/
public static String getBeginEndLocation(String strFile, int iStart,int iEnd) {
String strLocate = "";
if (iEnd < 0) {
return "";
}
try {
if (strFile.length() >= (iEnd)) {
strLocate = strFile.substring(iStart, iEnd);
} else if (strFile.length() >= iStart) {
strLocate = strFile.substring(iStart);
} else {
strLocate = "";
}
} catch (Exception e) {
System.out.println(e);
}
return strLocate.trim();// strFile2
}
/**
* getStringByLocation: 根据指定行的位置获取信息
* @param strFile 给定字串
* @param iStart 起始位置,
* @param iEnd 结束位置
* @param strAdd 后面增加指定字符
* @return
*/
public static String getBeginEndLocation(String strFile, int iStart,int iEnd,String strAdd) {
String strLocate = "";
try {
if (strFile.length() >= (iEnd)) {
strLocate = strFile.substring(iStart, iEnd);
} else if (strFile.length() >= iStart) {
strLocate = strFile.substring(iStart);
} else {
strLocate = "";
}
} catch (Exception e) {
System.out.println(e);
}
return strLocate.trim() + strAdd;// strFile2
}
/**
* getStringByLocation: 根据指定行的位置获取信息
* @param strFile 给定字串 从1开始计算的
* @param iStart 起始位置,
* @param iLen 截取的字串长度
* @return
*/
public static String getBeginEnd1Location(String strFile, int iStart,int iLen) {
String strLocate = "";
try {
if (strFile.length() >= (iStart + iLen)) {
strLocate = strFile.substring(iStart - 1, (iStart + iLen)
评论4