StringUtil

[b][color=red]开发者博客:[url]http://www.developsearch.com[/url][/color][/b]

/**
* 字符串工具类
*
* @author chenxin
* @version [版本号, 2012-5-21]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class StringUtil {


/**
* 判断字符串是否为空
*
* @param str
* @return
* @see [类、类#方法、类#成员]
*/
public static boolean isEmpty(String str)
{
return (str == null) || (str.trim().length() == 0);
}


/**
* 给字符串去掉空格
*/
public static String trim(String arg)
{
if (null == arg)
{
return "";
}
else
{
return arg.trim();
}
}


/**
* getLength 返回字符串的长度
*
* @param src 输入字符串
* @return int 字符串长度
*/
public static int getLength(String src)
{
return ((null == src) || ("".equals(src))) ? 0 : src.getBytes().length;
}


/**
* replace$ 返回字符串,将一个$更改为两个$
*
* @param instr 输入字符串
* @return String
*/
public static String replaceDollarMark(String instr)
{
StringBuffer sb = new StringBuffer(instr);
int place = sb.indexOf("$");
while (place >= 0)
{
sb.replace(place, place + 1, "$$");
place = sb.indexOf("$", place + 2);
}
return sb.toString();
}


/**
* 去掉字符串中的空格
*
* @param str
* @return String
*/
public static String removeBlank(String str) {
StringBuilder sb = new StringBuilder();
char c = ' ';
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch != c) {
sb.append(ch);
}
}
return sb.toString();
}


/**
* 将输入流转换为字符串
* 该方法需要慎用
* @param is 输入流
* @return
*/
public static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
int index = 0;
while ((line = reader.readLine()) != null) {
if(index++ > 0 ){
sb.append(System.getProperty("line.separator"));
}
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return sb.toString();
}


/**
* 获取字符串的长度
* @param aStr
* @return int
*/
public static int getStringLength(String aStr)
{
int len = aStr.length();
int result = 0;
char c;
for (int i = 0; i < len; i++)
{
c = aStr.charAt(i);
if (c > 256)
{
result += 2;
}
else
{
result += 1;
}
}
return result;
}


/**
* 判断字符串是否为整数
*
* @param s
* @see [类、类#方法、类#成员]
*/
public static boolean isInt(String s)
{
boolean result = true;
if (StringTools.isEmpty(s))
{
result = false;
return result;
}
try
{
Integer.parseInt(s);
}
catch (Exception e)
{
result = false;
}

return result;
}

/**
* 判断字符串是否为Long类型
*
* @param s
* @see [类、类#方法、类#成员]
*/
public static boolean isLong(String s)
{
boolean result = true;
if (StringTools.isEmpty(s))
{
result = false;
return result;
}
try
{
Long.parseLong(s);
}
catch (Exception e)
{
result = false;
}

return result;
}


/**
* 去掉两位的打折数字的末位0
*
* @param discount 折扣
* @return 折扣数
* @see [类、类#方法、类#成员]
*/
public static String convertDiscount(String discount)
{
if (discount == null || "".equals(discount.trim()))
{
return "";
}
else if (discount.endsWith("0"))
{
return discount.substring(0, discount.length() - 1);
}
else
{
return discount;
}
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值