java_method_StringUtils

/**
* @Title: StringUtils.java
* @Package cn.com.qmhd.tools
* @Description: TODO:
* @author eric
* @date 2014-11-17下午12:37:28
* @version V1.0
*/
package cn.com.qmhd.tools;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

public class StringUtils {

	/**
	 * 字符串特殊字符转换
	 * @param temp
	 * @return
	 */
	public static String getStringParameter(String temp){

		if ( temp == null || "".equals( temp ) ){
			return "";
		}
		temp = temp.replace( "&" , "&" );
		temp = temp.replace( "<" , "<" );
		temp = temp.replace( ">" , ">" );
		temp = temp.replace( "\"", """ );
		temp = temp.replace( "\‘", "′" );

		return temp;

	}

	/**
	 * 字符串英文单双引号处理
	 * @param temp
	 * @return
	 */
	public static String getStringDatabase(String temp){

		if ( temp == null || "".equals( temp )){
			return "";
		}
		temp = temp.replace( "‘" , "’" );
		temp = temp.replace("\"", "”");

		return temp;
	}

	/**
	 * To determine whether the string is empty or null
	 * @param temp Need the string to judge
	 * @return true is not null or empty ; false is null or empty
	 */
	public static boolean isEmpty(String temp){

		if ( temp!=null && !"".equals(temp) ) {
			return true;
		}
		return false;
	}

	/**
	 * To determine whether the string is null
	 * @param temp Need the string to judge
	 * @return if temp is null back empty string else back temp;
	 */
	public static String isNull(String temp){

		if( temp==null ){
			return "";
		}
		return temp;
	}

	/**
	 * Whether the same string comparison
	 * @param temp1 string a
	 * @param temp2 string b
	 * @return true,string same;false,string not the same or appear exception
	 */
	public static boolean equals(String temp1,String temp2){

		try {
			if (temp1.equals(temp2)) {
				return true;
			}
		} catch (Exception e) {
			Log.getInstance().printError(new StringUtils().getClass().getName(), "equals string error");
		}
		return false;
	}

	/**
	 * Cut the string
	 * @param temp : the original string
	 * @param splitsign : separator
	 * @return String[] : segmented after string array
	 *                    null : appear exception
	 */
	public static String[] split(String temp,String splitsign){

		try {
			String[] temps = temp.split(splitsign);
			return temps;
		} catch (Exception e) {
			Log.getInstance().printError(new StringUtils().getClass().getName(), "split string error");
		}
		return null;
	}

	/**
	 * Cut the string
	 * @param temp : the original string
	 * @param oldChar : The need to replace string
	 * @param newChar : The target string of replacement
	 * @return String : segmented after string array
	 *         null : appear exception
	 */
	public static String replace(String temp,String oldChar,String newChar){
		try {
			String newTemp = temp.replace(oldChar, newChar);
			return newTemp;
		} catch (Exception e) {
			Log.getInstance().printError(new StringUtils().getClass().getName(), "split string error");
		}
		return null;
	}

	/**
	 * string forced transcoding int
	 * @param temp : The need transcoding of string
	 * @return int : The conversion to int type
	 * @throws Exception :NumberFormatException
	 */
	public static int stringToInt(String temp) throws Exception {
		try {
			return Integer.parseInt(temp);
		} catch (NumberFormatException e) {
			String sException = ExceptionHeading.getException(new StringUtils().getClass().getName(), e, "string to int error");
			throw new Exception(sException);
		}
	}

	/**
	 * Get UUID
	 * @return int : UUID To String
	 */
	public static String getUUID(){
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	/**
	 * @param request
	 * @return
	 */
	public static String getIp(HttpServletRequest request) {
		if (request.getHeader("x-forwarded-for") == null) {
			return request.getRemoteAddr();
		}else {
			return request.getHeader("x-forwarded-for");
		}
	}

  /**
   * @Descrption 组合模版短信
   * @author eric
   * @date 2016-11-3下午1:50:35
   * @param StringUtils
   * @return String
   */
  public static String templateForAllContent(String strContent, String strTempContent){
    StringBuffer strbContent=new StringBuffer();
    String[] strsTempContent = strTempContent.split("\\|");
    for (int i = 0; i < strsTempContent.length; i++) {
      if (strContent.indexOf("{txt}")==-1) {
        break;
      }
      strbContent.append(strContent.substring(0, strContent.indexOf("{txt}")));
      strbContent.append(strsTempContent[i]);
      strContent = strContent.substring(strContent.indexOf("{txt}")+5);
    }
    strbContent.append(strContent);
    return strbContent.toString().replace("{txt}", "");
  }
}

  

时间: 2024-10-20 20:19:33

java_method_StringUtils的相关文章