`
keepwork
  • 浏览: 327148 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论
阅读更多
开发者博客:http://www.developsearch.com

/**
 * 字符串工具类 
 * 
 * @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;
        }
    }





}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics