import java.awt.Color;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;
public class StringUtils
{
public StringUtils()
{
}
/**
* 判断s是否包含sTobeContains
*/
public static boolean containsIgnoreCase(String s, String sTobeContains)
{
return StringUtils.contains(s.toLowerCase(), sTobeContains.toLowerCase());
}
public static boolean contains(String s, String sTobeContains)
{
return s.indexOf(sTobeContains) != -1;
}
/**
* 将字符串用spliter分解为一个数组
* @param s 要分割的字符串
* @param spliter 用于分割的字符
* @return
*/
public static String[] splitByTokenizer(String s, String spliter)
{
StringTokenizer strToken = new StringTokenizer(s, spliter, false);
StringTokenizer childToken = null;
int tokenNum = strToken.countTokens();
String sa[] = new String[tokenNum];
int i = 0;
while (strToken.hasMoreTokens())
{
childToken = new StringTokenizer(strToken.nextToken());
sa[i++] = childToken.nextToken();
}
return sa;
// Vector v=new Vector();
// String temp=s;
// int index,len=spliter.length();
// while((index=temp.indexOf(spliter))!=-1){
// v.addElement(temp.substring(0,index));
// temp=temp.substring(index+len);
// }
// v.addElement(temp);
// String[] rs=new String[v.size()];
// for(int i=0;i
// }
// return rs;
}
/**
* 将字符串用spliter分解为一个数组,和splitByTokenizer 比分割的算法不同
* @param s 要分割的字符串
* @param spliter 用于分割的字符
* @return
*/
public static String[] split(String s, String spliter)
{
Vector v = new Vector();
String temp = s;
int index, len = spliter.length();
while ( (index = temp.indexOf(spliter)) != -1)
{
v.addElement(temp.substring(0, index));
temp = temp.substring(index + len);
}
v.addElement(temp);
String[] rs = new String[v.size()];
for (int i = 0; i < v.size(); i++)
{
rs[i] = (String) v.elementAt(i);
}
return rs;
}
/**
* 将字符串转换为java.awt.Color对象
* 传入的字符串必须是r,g,b 格式的
* r.g.b必须是整数。
*/
public static Color createColor(String colorString)
{
Color c = null;
if (colorString.startsWith("#"))
{
String paramValue = colorString.substring(1);
int red;
int green;
int blue;
red = (Integer.decode("0x" + paramValue.substring(0, 2))).intValue();
green = (Integer.decode("0x" + paramValue.substring(2, 4))).
intValue();
blue = (Integer.decode("0x" + paramValue.substring(4, 6))).intValue();
return new Color(red, green, blue);
}
if (colorString.equalsIgnoreCase("white"))
{
c = Color.white;
}
else if (colorString.equalsIgnoreCase("lightGray"))
{
c = Color.lightGray;
}
else if (colorString.equalsIgnoreCase("gray"))
{
c = Color.gray;
}
else if (colorString.equalsIgnoreCase("darkGray"))
{
c = Color.darkGray;
}
else if (colorString.equalsIgnoreCase("black"))
{
c = Color.black;
}
else if (colorString.equalsIgnoreCase("red"))
{
c = Color.red;
}
else if (colorString.equalsIgnoreCase("pink"))
{
c = Color.pink;
}
else if (colorString.equalsIgnoreCase("orange"))
{
c = Color.orange;
}
else if (colorString.equalsIgnoreCase("yellow"))
{
c = Color.yellow;
}
else if (colorString.equalsIgnoreCase("green"))
{
c = Color.green;
}
else if (colorString.equalsIgnoreCase("magenta"))
{
c = Color.magenta;
}
else if (colorString.equalsIgnoreCase("cyan"))
{
c = Color.cyan;
}
else if (colorString.equalsIgnoreCase("blue"))
{
c = Color.blue;
}
else
{
String sa[] = StringUtils.split(colorString, ",");
if (sa.length == 3)
{
c = new Color(Integer.parseInt(sa[0]), Integer.parseInt(sa[1]),
Integer.parseInt(sa[2]));
}
if (c == null)
{
c = Color.black;
}
}
return c;
}
/**
* 用replacer替换s中的tobeReplaced字符串
*/
public static String replace(String s, String tobeReplaced, String replacer)
{
String right = "";
StringBuffer sr = new StringBuffer("");
int index = -1;
String sb = tobeReplaced
, st = replacer;
if (s == "" || sb == "")
{
return s;
}
right = s;
while ( (index = right.indexOf(sb)) != -1)
{
sr.append(right.substring(0, index)).append(st);
right = right.substring(index + sb.length());
}
return sr.append(right).toString();
}
/**
* 把String转化为double
*/
public static double stringToDouble(String s)
{
return Double.valueOf(s).doubleValue();
}
public static boolean stringToBoolean(String booleanValue)
{
return Boolean.valueOf(booleanValue).booleanValue();
}
/**
* 把String转化为ISO-8859-1编码的字符。
*/
public static String getGBString(String s)
{
String rs = "";
if (s != null)
{
try
{
rs = new String(s.getBytes("ISO8859-1"));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
return rs;
}
/**
*把sourceStr格式化输出为
* @param sourceStr
* @return
*/
public static String toHtmlString(String sourceStr)
{
String target = StringUtils.replace(sourceStr, "<", "<");
target = StringUtils.replace(target, ">", ">");
//target=StringTools.replace(target," "," ");
return target;
}
/**
*把sourceStr格式化输出在网页当中
* @param sourceStr
* @return
*/
public static String toHtmlStringBr(String sourceStr)
{
String target = StringUtils.toHtmlString(sourceStr);
target = StringUtils.replace(target, "\n", "
");
StringBuffer str = new StringBuffer(target);
return target;
}
/**
*将格式化的html字符串复原
* @param sourceStr
* @return
*/
public static String htmlToString(String sourceStr)
{
String target = StringUtils.replace(sourceStr, "<", "<");
target = StringUtils.replace(target, ">", ">");
//target=StringTools.replace(target," "," ");
return target;
}
public static String htmlToStringBr(String sourceStr)
{
sourceStr = StringUtils.htmlToString(sourceStr);
sourceStr = StringUtils.replace(sourceStr, "
", "\n");
return sourceStr;
}
/**
* 去掉sourceStr右边的空格
* @param sourceStr
* @return
*/
public static String trimRt(String sourceStr)
{
int i = sourceStr.length();
char space = ' ';
for (i = i - 1; i >= 0; i--)
{
if (sourceStr.charAt(i) != space)
{
break;
}
}
return sourceStr.substring(0, i + 1);
}
/**
*生成网页图片的字符串,可以对.swf文件进行处理
* @param imagename 图片源文件名
* @param style 图片风格
* @param w 图片宽
* @param h 图片高
* @return
*/
public static String getImageString(String imagename, String style, int w,
int h)
{
if (imagename == null || "".equals(imagename))
{
return "";
}
String imagestr = imagename;
StringBuffer strbuf = new StringBuffer();
if (imagestr.toLowerCase().endsWith(".swf"))
{
if (w == -1 && h == -1)
{
strbuf.append(
"
/**
* 将双精度的数字转成指定小数位数的字符
* @param num 双精度数
* @param n 小数位数
* @return num对应的字符
*/
public static String getDoubleString(double num, int n)
{
String str = Double.toString(num);
if (str != null && !"".equals(str))
{
int i = str.indexOf(".");
if (i + n < str.length())
{
return (str.substring(0, i + n + 1));
}
return (str.substring(0, str.length()));
}
return str;
}
/**
* 取request中所有参数名前缀为pre,分隔符为split后的字符串,返回一个Arraylist的参数集合
* @param request
* @param pre
* @param split
* @return
*/
public static ArrayList getRegPreString(javax.servlet.http.
HttpServletRequest request,
String pre, String split)
{
ArrayList list = new ArrayList();
if (split == null || "".equals(split))
{
split = "_";
}
Enumeration enum1 = request.getParameterNames();
while (enum1.hasMoreElements())
{
String name = (String) enum1.nextElement();
int index = name.indexOf(split);
if (index != -1)
{
if (name.substring(0, index).equals(pre))
{
list.add(name.substring(index + 1));
}
}
}
if (list.size() <= 0)
{
return null;
}
return list;
}
/**
* * 为了sql 查询使用 like运算,在前后加%符号
* @param str
* @return
*/
public static String likeStr(String str){
if( isStrEmpty(str)){
return "%";
}
else{
return new StringBuilder("%").append(str.trim()).append("%").toString();
}
}
/**
* 为了sql 查询使用 like运算,将字符首先变大写的,然后再在前后加%符号
* @param str
* @return
*/
public static String likeUpperStr(String str){
if( isStrEmpty(str)){
return "%";
}
else{
return new StringBuilder("%").append(str.trim().toUpperCase()).append("%").toString();
}
}
public static boolean isStrEmpty(String str){
return str== null || str.trim().length() == 0;
}
/**
* 首字母大写
* @param str
* @return str的首字母变大写
*/
public static String captionUpper(String str)
{
if(isStrEmpty(str))
{
return "";
}
else
{
return str.substring(0,1).toUpperCase() + str.substring(1);
}
}
/**
* 首字母小写
* @param str
* @return str的首字母小写
*/
public static String captionLower(String str)
{
if(isStrEmpty(str))
{
return "";
}
else
{
return str.substring(0,1).toLowerCase() + str.substring(1);
}
}
/**
* 判断字符串是否为数字
* @param fromCurrency
* @param toCurrency
* @param con
* @return
* @throws Exception
*/
public static boolean isNumber(String str)
{
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("^[\\-|\\+]?\\d+$|^[\\-|\\+]?\\d+\\.\\d*$");
java.util.regex.Matcher matcher = pattern.matcher(str);
if (matcher.matches())
{
return true;
}
else
{
return false;
}
}
public static void main(String args[]){
System.out.println(StringUtils.likeStr(null));
System.out.println(StringUtils.likeStr(""));
System.out.println(StringUtils.likeStr("a"));
System.out.println(StringUtils.captionUpper(null));
System.out.println(StringUtils.captionUpper(""));
System.out.println(StringUtils.captionUpper("a"));
System.out.println(StringUtils.captionUpper("abcd"));
}
}