📄 stringutils.java
字号:
package gov.gdlt.ssgly.taxcore.comm.util;
import java.util.Date;
import java.util.Calendar;
import java.util.Locale;
import java.util.ArrayList;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.math.BigDecimal;
/**
*字符串处理函数工具包
*
* <p>Title: StringUtils</p>
* <p>Description: 广东地税大集中项目新一代税收征管信息系统</p>
* <p>Copyright: Copyright (c) 2003 广东省地方税务局, 中软网络技术股份有限公司</p>
* <p>Company: 中软网络技术股份有限公司</p>
* @author 刘付伟
* @version 1.0
*/
public class StringUtils
{
public StringUtils(){}
/**
* 字符串的字符集类型转换
* @param src 需要转换的字符串
* @param fromCharSet 字符串当前的字符集类型,如"iso-8859-1","GBK"等
* @param toCharSet 目标字符集类型,如"iso-8859-1","GBK"等
* @return 转换后的字符串,失败返回null
*/
public static String charSetConvert(String src,String fromCharSet,String toCharSet){
if(src == null)
return src;
try{
return new String(src.getBytes(fromCharSet),toCharSet);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
/**
* 将iso8859的字符集转换成UTF-8字符集
* @param src iso8859字符串
* @return 转化后的字符串,失败返回null
*/
public static String isoToUTF8(String src){
return charSetConvert(src,"iso-8859-1","UTF-8");
}
/**
* 将iso8859的字符集转换成GBK字符集
* @param src iso8859字符串
* @return 转化后的字符串,失败返回null
*/
public static String isoToGBK(String src){
return charSetConvert(src,"iso-8859-1","GBK");
}
/**
* 将GBK的字符集转换成iso8859字符集
* @param src GBK字符串
* @return 转化后的字符串,失败返回null
*/
public static String gbkToISO(String src){
return charSetConvert(src,"GBK","iso-8859-1");
}
/**
* 字符替换函数
* <br>比如 String a = "ccddaa"; replace(a,"dd","xx") 将返回 ccxxaa
* @param str 需要替换的原始字符串
* @param pattern 需要被替换掉的字符串
* @param replace 希望被替换成的字符串
* @return 返回替换后的字符串
*/
public static String replace(String str, String pattern, String replace) {
int s = 0;
int e = 0;
StringBuffer result = new StringBuffer();
while ((e = str.indexOf(pattern, s)) >= 0) {
result.append(str.substring(s, e));
result.append(replace);
s = e+pattern.length();
}
result.append(str.substring(s));
return result.toString();
}
/**
* 重复一个字符串 n 次,比如 abcabcabc
*
* @param str 需要重复的字符串
* @param repeat 重复的次数
* @return 重复后生成的字符串
*/
public static String repeat(String str, int repeat) {
StringBuffer buffer = new StringBuffer(repeat * str.length());
for (int i = 0; i < repeat; i++) {
buffer.append(str);
}
return buffer.toString();
}
/**
* 获得一个字符串的最左边的n个字符,如果长度len的长度大于字符串的总长度,返回字符串本身
*
* @param str 原始的字符串
* @param len 左边的长度
* @return 最左边的字符
*/
public static String left(String str, int len) {
if (len < 0) {
throw new IllegalArgumentException("Requested String length " + len + " is less than zero");
}
if ((str == null) || (str.length() <= len)) {
return str;
} else {
return str.substring(0, len);
}
}
/**
* 获得一个字符串的最右边的n个字符,如果长度len的长度大于字符串的总长度,返回字符串本身
*
* @param str 原始的字符串
* @param len 右边的长度
* @return 最右边的字符
*/
public static String right(String str, int len) {
if (len < 0) {
throw new IllegalArgumentException("Requested String length " + len + " is less than zero");
}
if ((str == null) || (str.length() <= len)) {
return str;
} else {
return str.substring(str.length() - len);
}
}
/**
* 将给定的字符串格式化为定长字符串, 原始字符串长度超过给定长度的,按照给定长度从左到右截取
* 如果原始字符串小于给定长度, 则按照给定字符在左端补足空位
* @param src 原始字符串
* @param s2 补充用字符,
* @param length 格式化后长度
* @return 格式化后字符串
*/
public static String formatString(String src, char s2, int length){
String retValue = src;
if(src == null || length <=0){
return null;
}
if(src.length() > length){
retValue = src.substring(0,length);
}
for(int i=0; i<length - src.length(); i++){
retValue = s2 + retValue;
}
return retValue;
}
/**
* 将一个浮点数转换为人民币的显示格式:¥##,###.##
* @param value 浮点数
* @return 人民币格式显示的数字
*/
public static String toRMB(double value) {
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
return nf.format(value);
}
/**
* 默认保留小数点后两位,将一个浮点数转换为定长小数位的小数 ######.##
* @param value 浮点数
* @return 定长小数位的小数
*/
public static String toCurrencyWithoutComma(double value){
String retValue = toCurrency(value);
retValue = retValue.replaceAll(",","");
return retValue;
}
/**
* 默认保留小数点后两位,将一个浮点数转换为货币的显示格式:##,###.##
* @param value 浮点数
* @return 货币格式显示的数字
*/
public static String toCurrency(double value) {
return toCurrency(value,2);
}
/**
* 根据指定的小数位数,将一个浮点数转换为货币的显示格式
* @param value 浮点数
* @param decimalDigits 小数点后保留小数位数
* @return 货币格式显示的数字
* <br>
* <br>例:
* toCurrency(123456.789,5) 将返回 "123,456.78900"
*/
public static String toCurrency(double value,int decimalDigits) {
String format = "#,##0." + repeat("0",decimalDigits);
NumberFormat nf = new DecimalFormat(format);
return nf.format(value);
}
/**
* 将一个字符串格式化为给定的长度,过长的话按照给定的长度从字符串左边截取,反之以给定的
* 字符在字符串左边补足空余位
* <br>比如:
* <br>prefixStr("abc",'0',5) 将返回 00aaa
* <br>prefixStr("abc",'0',2) 将返回 ab
* @param source 原始字符串
* @param profix 补足空余位时使用的字符串
* @param length 格式化后字符串长度
* @return 返回格式化后的字符串,异常返回null
*/
public static String prefixStr(String source,char profix,int length){
String strRet = source;
if(source == null){
return strRet;
}
if(source.length() >= length){
strRet = source.substring(0,length);
}
if(source.length() < length){
for(int i=0;i<length-source.length();i++){
strRet = "" + profix + strRet;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -