Wednesday, May 23, 2012

String util services for String handling in java


public static boolean equalsIgnoreCase(String str1, String str2) {
        return ((str1 == null) ? (str2 == null) : str1.equalsIgnoreCase(str2));
    }
 public static boolean equals(String str1, String str2) {
        return ((str1 == null) ? (str2 == null) : str1.equals(str2));
    }

    public static String trim(String str) {
        return ((str == null) ? null : str.trim());
    }

   public static boolean isNotEmpty(String str) {
        return ((str != null) && (str.length() > 0));
    }

  public static boolean isEmpty(String str) {
        return ((str == null) || (str.length() == 0));
    }


    public static boolean isBlank(String str) {
        int strLen;

        if ((str == null) || ((strLen = str.length()) == 0)) {
            return true;
        }

        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }

        return true;
    }

 public static boolean isNotBlank(String str) {
        int strLen;

        if ((str == null) || ((strLen = str.length()) == 0)) {
            return false;
        }

        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return true;
            }
        }

        return false;
    }
public static boolean equalNullOrTrimEmpty(String str1, String str2){
        return (str1 == null
                    ? (str2 == null
                            ? true
                            : (str2.trim().equals("")
                                ? true
                                : false))
                    : (str2 == null
                            ? (str1.trim().equals("")
                                ? true
                                : false)
                            : (str1.trim().equals(str2.trim()))
                        )
                );                        
    }

public static boolean equalNullOrTrimEmptyIgnoreCase(String str1, String str2){
        return (str1 == null ? (str2 == null ? true : (str2.trim().equals("") ? true : false)): (str2 == null? (str1.trim().equals("")? true: false): (str1.trim().equalsIgnoreCase(str2.trim()))));                        
    }


public static String getFormattedAmount(double param) {
        if(param == 0.0)
            return "$0.00";
        return NumberFormat.getCurrencyInstance().format(param);   
    }
   
    public static String getFormattedAmount(String param) {
        if(isEmpty(param))
            return "";
        if (param.startsWith("$")){
            return param;
        }
        try{
            double f1=Double.parseDouble(param);
            return NumberFormat.getCurrencyInstance().format(f1);   
        }catch(NumberFormatException e){
            return param;
        }
    }
   
    public static String getPercentage(double val){
        return NumberFormat.getPercentInstance().format(val);
    }
   
    public static String getPercentage(int actualVal, int totalVal){
        return getPercentage((1.0*actualVal)/(1.0*totalVal));
    }   



 public static String getAlpha(String str) {
        if ((str == null) || isEmpty(str)) {
            return null;
        }

        StringBuffer buf = new StringBuffer(str.length());
        int sz = str.length();

        for (int i = 0; i < sz; i++) {
            if (Character.isLetter(str.charAt(i)) == true) {
                buf.append(str.charAt(i));
            }
        }

        return buf.toString();
    }


public static String getAlphaNumeric(String str) {
        if ((str == null) || isEmpty(str)) {
            return null;
        }

        StringBuffer buf = new StringBuffer(str.length());
        int sz = str.length();

        for (int i = 0; i < sz; i++) {
            if (Character.isLetterOrDigit(str.charAt(i)) == true) {
                buf.append(str.charAt(i));
            }
        }

        return buf.toString();
    }

 public static String getAlphaNumericStar(String str) {
        if ((str == null) || isEmpty(str)) {
            return null;
        }

        StringBuffer buf = new StringBuffer(str.length());
        int sz = str.length();

        for (int i = 0; i < sz; i++) {
            if ((Character.isLetterOrDigit(str.charAt(i)) == true) ||
                    (str.charAt(i) == '*')) {
                buf.append(str.charAt(i));
            }
        }

        return buf.toString();
    }

No comments:

Post a Comment