A Utility class to manipulate Strings.
Contains methods to do find-replace, find-replace-all, Padding(Left pad & Right pad), Special Custom Tokenizer, Trim, Truncate, Check Null, NVL method... and so on.
Many of these methods are customized to cater to specific application needs.
Contains methods to do find-replace, find-replace-all, Padding(Left pad & Right pad), Special Custom Tokenizer, Trim, Truncate, Check Null, NVL method... and so on.
Many of these methods are customized to cater to specific application needs.
import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.StringTokenizer; import java.util.Vector; /** * A Utility to manipulate Strings. Contains methods to do find-replace, pad, * tokenize and truncate. Some methods are customized to cater to specific needs. * */ public final class StringUtil extends Object { /** * Finds the string findStr in the String strValue and replaces it with * replaceStr. <br> * Usage: <br> * <code>StrUtil.findReplace("abcdefg-abcdefg","abcd","dcba")</code> * returns dcbaefg-abcdefg. * * @param strValue * java.lang.String * @param findStr * java.lang.String * @param replaceStr * java.lang.String * @return java.lang.String */ public static String findReplace(String strValue, String findStr, String replaceStr) { StringBuffer sbuf = new StringBuffer(strValue); int idx1 = strValue.indexOf(findStr); if (idx1 == -1) { return strValue; } sbuf.replace(idx1, idx1 + findStr.length(), replaceStr); return sbuf.toString(); } /** * Finds the string findStr in the String strValue and replaces all the * occurrances of it with the replaceStr. <br> * Usage: <br> * <code>StrUtil.findReplace("abcdefg-abcdefg","abcd","12")</code> returns * 12efg-12efg. * * @param strValue * java.lang.String * @param findStr * java.lang.String * @param replaceStr * java.lang.String * @return java.lang.String */ public static String findReplaceAll(String strValue, String findStr, String replaceStr) { StringBuffer sbuf = new StringBuffer(strValue); int idx1 = strValue.indexOf(findStr); int fromIdx = 0; if (idx1 == -1) { return strValue; } do { sbuf.replace(idx1, idx1 + findStr.length(), replaceStr); strValue = sbuf.toString(); fromIdx = idx1 + replaceStr.length(); idx1 = strValue.indexOf(findStr, fromIdx); } while (idx1 != -1); return sbuf.toString(); } /** * Returns true if the strVal is not equals to null or a zero length string * "". * * @param strVal * java.lang.String * @return boolean */ public static boolean isNotNull(String strVal) { return !isNull(strVal); } /** * Returns true if the strVal is equals to null or a zero length string "". * * @param strVal * java.lang.String * @return boolean */ public static boolean isNull(String strVal) { if ((strVal == null) || (strVal.trim().equals(""))) { return true; } else { return false; } } /** * Padds the String to its left with spaces until the string reaches a * length specified by strLen. <br> * Usage: <br> * <code>StrUtil.lPad("efgh",8)</code> returns * * <pre> * efgh * </pre>. * * @param str * java.lang.String * @param strLen * int * @return java.lang.String * @see #lPad(String, int, char) */ public static String lPad(String str, int strLen) { return lPad(str, strLen, ' '); } /** * Padds the String to its left with the padding character passed until the * string reaches a length specified by strLen. <br> * Usage: <br> * <code>StrUtil.lPad("efgh",8,'0')</code> returns * * <pre> * 0000efgh * </pre>. * * @param str * java.lang.String * @param strLen * int * @param padChar * char * @return java.lang.String * @see #lPad(String, int) */ public static String lPad(String str, int strLen, char padChar) { StringBuffer result = new StringBuffer(""); if (str == null) { str = ""; } int inpLen = str.length(); if ((strLen <= 0) || (inpLen >= strLen)) { return str; } for (int i = inpLen; i < strLen; i++) { result.append(padChar); } result.append(str); return result.toString(); } /** * Returns str1 if str1 is not null else str2. * * @param str1 * java.lang.String * @param str2 * java.lang.String * @return java.lang.String */ public static String nullValue(String str1, String str2) { if (str1 == null) { return str2; } return str1; } /** * Replaces the findChar with the replaceChar in the String str. <br> * Example: <code>StrUtil.replaceChars("abcdefg-abcdefg",'b','*')</code> * returns a*cdefg-a*cdefg. * * @param str * java.lang.String * @param findChar * char * @param replaceChar * char * @return java.lang.String */ public static String replaceChars(String str, char findChar, char replaceChar) { char[] chArray = str.toCharArray(); int len = chArray.length; for (int i = 0; i < len; i++) { if (chArray[i] == findChar) { chArray[i] = replaceChar; } } //return new String(chArray); return chArray.toString(); } /** * Padds the String to its right with spaces until the string reaches a * length specified by strLen. <br> * Usage: <br> * <code>StrUtil.rPad("abcd",8)</code> returns * * <pre> * abcd * </pre>. * * @param str * java.lang.String * @param strLen * int * @return java.lang.String * @see #rPad(String, int, char) */ public static String rPad(String str, int strLen) { return rPad(str, strLen, ' '); } /** * Padds the String to its right with the padding character passed until the * string reaches a length specified by strLen. <br> * Usage: <br> * <code>StrUtil.rPad("abcd",8,'0')</code> returns * * <pre> * abcd0000 * </pre>. * * @param str * java.lang.String * @param strLen * int * @param padChar * char * @return java.lang.String * @see #rPad(String, int) */ public static String rPad(String str, int strLen, char padChar) { StringBuffer result = new StringBuffer(""); if (str == null) { str = ""; } int inpLen = str.length(); if ((strLen <= 0) || (inpLen >= strLen)) { return str; } result.append(str); for (int i = inpLen; i < strLen; i++) { result.append(padChar); } return result.toString(); } /** * Tokenizes the String str with the delimiter. <br> * This method does not skip the empty tokens. <br> * Example: <br> * <code>StrUtil.tokenize("a::bc:d:",':')</code> returns * * <pre> * [a, , bc, d, ] * </pre>. * * @param str * java.lang.String * @param delimiter * char * @return java.util.Vector */ public static Vector tokenize(String str, char delimiter) { Vector values = new Vector(); int currentPosition = 0; int maxPosition = str.length(); while (currentPosition < maxPosition) { while ((currentPosition < maxPosition) && (delimiter == str.charAt(currentPosition))) { currentPosition++; values.add(""); } int start = currentPosition; while ((currentPosition < maxPosition) && (delimiter != str.charAt(currentPosition))) { currentPosition++; } values.add(str.substring(start, currentPosition)); if ((currentPosition == (maxPosition - 1)) && (delimiter == str.charAt(currentPosition))) { values.add(""); break; } currentPosition++; } return values; } /** * Tokenizes the String str with any of the delimiters passed. <br> * This method does not skip the empty tokens. <br> * Example: <br> * <code>StrUtil.tokenize("a:*bc#d:",":*#")</code> returns * * <pre> * [a, , bc, d, ] * </pre>. * * @param str * java.lang.String * @param delimiters * java.lang.String * @return java.util.Vector */ public static Vector tokenize(String str, String delimiters) { Vector values = new Vector(); int currentPosition = 0; int maxPosition = str.length(); while (currentPosition < maxPosition) { while ((currentPosition < maxPosition) && (delimiters.indexOf(str.charAt(currentPosition)) >= 0)) { currentPosition++; values.add(""); } int start = currentPosition; while ((currentPosition < maxPosition) && (delimiters.indexOf(str.charAt(currentPosition)) < 0)) { currentPosition++; } values.add(str.substring(start, currentPosition)); if ((currentPosition == (maxPosition - 1)) && (delimiters.indexOf(str.charAt(currentPosition)) >= 0)) { values.add(""); break; } currentPosition++; } return values; } /** * Truncated the String to the specified size. <br> * Example: <br> * <code>StrUtil.truncate("1234567890",5)</code> returns * <pre>12345</pre>. * * @param str * java.lang.String * @param size * int * @return java.lang.String */ public static String truncate(String str, int size) { if (str == null) { return null; } if (str.length() <= size) { return str; } return str.substring(0, size); } /** * Translates a string to a valid XML string. Replaces the character "&" * to "<code>&amp;</code>". * * @param str * String that needs to be translated. * @return translated string. */ public static String translateToValidXml(String str) { String finalStr = str; try { if (isNotNull(finalStr)) { finalStr = findReplaceAll(str, "&", "&"); finalStr = findReplaceAll(finalStr, "<", "<"); finalStr = findReplaceAll(finalStr, ">", ">"); finalStr = findReplaceAll(finalStr, """, """); } } catch (Exception e) { } return finalStr; } /** * Method tuncateString. this function retuens the string in the rounded * format. * * @param input * is input String * @param Number * is the int number for rounding. * @return String */ public static String truncateString(String input, int number) { try { DecimalFormatSymbols dfs = new DecimalFormatSymbols(); dfs.setDecimalSeparator('.'); DecimalFormat formatter = new DecimalFormat("#0.0000", dfs); formatter.setMaximumFractionDigits(number); input = formatter.format(Double.parseDouble(input)); } catch (Exception e) { } return input; } /** * This function returns the string after truncating the decimal and decimal * seperator. * * @param input * is input String * @param Number * is the int number for rounding. * @return String */ public static String truncateDecimal(String input) { try { StringTokenizer stringTokenizer = new StringTokenizer(input, ","); String part1 = stringTokenizer.nextToken(); input = findReplaceAll(part1, ".", ""); } catch (Exception e) { } return input; } /** * Method compare. This function returns flag true if both strings are same, * it returns false for any other case. * * @param input * is input String * @param input * is input String * @return String * @author 142519 */ public static boolean isEqual(String str1, String str2) { boolean flag = false; if (str1 != null && str2 != null && str1.equals(str2)) { flag = true; } return flag; } /** * This function returns true if the strings passed in the argument are not same. * Otherwise it returns false. * * @param str1 * is input String * @param str2 * is input String * @return String */ public static boolean isNotEqual(String str1, String str2) { boolean flag = false; if (!(isNull(str1) && isNull(str2)) && (isNull(str1) || isNull(str2) || !str1.equals(str2))) { flag = true; } return flag; } /** * This function returns the string after truncating the decimal and decimal seperator. * * @param input * is input String * @param Number * is the int number for rounding. * @return String */ public static String truncateDecimalEn(String input) { String part2 = null; try { StringTokenizer stringTokenizer = new StringTokenizer(input, "."); //String part1 = stringTokenizer.nextToken(); part2 = stringTokenizer.nextToken(); } catch (Exception e) { } return part2; } /** * This method splits the input string and returns String array * * @param input * @return */ public static String[] splitString(String input) { String[] splitString = null; if (isNotNull(input)) { if (input.indexOf(",") != -1) { splitString = new String[2]; StringTokenizer stringTokenizer = new StringTokenizer(input, ","); splitString[0] = stringTokenizer.nextToken(); splitString[1] = "," + stringTokenizer.nextToken(); } else { splitString = new String[1]; splitString[0] = input; } } return splitString; } /** * Method valueOf This function returns null if value is less than or equal to 0. * * @return String */ public static String valueOf(long val) { if (val > 0) return String.valueOf(val); else return null; } /** * Method valueOf This function returns null if value is less than or equal to 0 * * @return String */ public static String valueOf(short val) { if (val > 0) return String.valueOf(val); else return null; } /** * Method valueOf This function returns null if value is less than or equal to 0 * * @return String */ public static String valueOf(double val) { if (val > 0) return String.valueOf(val); else return null; } /** * This method parses the string to int primitive value. * In case of errors 0 is returned. Exception is not thrown. * * @param strLongVal * @return long */ public static int parseIntQuietly(String strLongVal) { int iVal = 0; try { if (strLongVal != null) iVal = Integer.parseInt(strLongVal); } catch (Exception e) { // Be quiet. Shhhhh!!! } return iVal; } /** * This method parses the string to long primitive value. In case of errors * 0 is returned. Exception is not thrown. * * @param strLongVal * @return long */ public static long parseLongQuietly(String strLongVal) { long lVal = 0; try { if (strLongVal != null) lVal = Long.parseLong(strLongVal); } catch (Exception e) { // Be quiet. Shhhhh!!! } return lVal; } /** * This method parses the string to double primitive value. In case of * errors 0 is returned. Exception is not thrown. * * @param strLongVal * @return long */ public static double parseDoubleQuietly(String strDoubleVal) { double dVal = 0; try { if (strDoubleVal != null) dVal = Double.parseDouble(strDoubleVal); } catch (Exception e) { // Be quiet. Shhhhh!!! } return dVal; } /** * This method parses the boolean string to boolean primitive value. If the * parameter <code>str</code> is null or invalid, false is returned. * * @param str * @return */ protected static boolean parseBoolean(String str) { return parseBoolean(str, false); } /** * This method parses the boolean string to boolean primitive value. If the * parameter <code>str</code> is null or invalid, * <code>defaultValue</code> is returned. * * @param str * @param defaultValue * @return */ public static boolean parseBoolean(String str, boolean defaultValue) { boolean value = defaultValue; if (str != null && "true".equals(str.trim().toLowerCase())) value = true; return value; } }