Code Robo
Virtual Service
Comparator
Tester
Converter
Utility
Java Code Complience
Validator
Formatter
EncoderDecoder



Java String Utility Functions
       Talk to EasyAssistant

Java String Utility Functions. It provides sample java string functions. Copuple of examples are shown for Java String function which can be copied from here and used in any java project. . Sample code uses java.util.StringTokenizer class to tokenize the string.


Java String Utility File Content:

package sg.easy.util;

import java.util.StringTokenizer;

import org.apache.log4j.Logger;

public class StringUtilForEZLOnline {
	private static Logger logger = Logger.getLogger(StringUtilForEZLOnline.class.getName());

	public static boolean isNullOrEmpty(String str) {

		if (str == null)
			return true;

		if (str.trim().equals("")) {
			return true;
		} else {
			return false;
		}

	}

	public static boolean isNotNullOrEmpty(String str) {

		return (!isNullOrEmpty(str));

	}

	public static String getTrimedValue(String str) {

		if (str == null)
			return "";
		return str.trim();

	}

	public static String getTrimedValOrZeor(String str) {

		if (isNullOrEmpty(str)) {
			return "0";

		} else {
			return str.trim();
		}
	}

	public static double convertToDouble(String str) {

		if (isNullOrEmpty(str)) {
			return 0;

		} else {
			return Double.parseDouble(str);
		}
	}

	public static boolean isAllUpperCase(String phrase) {
		boolean flag = true;
		for (int i = 0; i < phrase.length(); i++) {
			char charAt = phrase.charAt(i);
			if ((charAt >= 65) && (charAt <= 91)) {

			} else {
				flag = false;
				break;
			}
		}

		return flag;
	}

	public boolean isAllLowerCase(String phrase) {
		boolean flag = true;
		for (int i = 0; i < phrase.length(); i++) {
			char charAt = phrase.charAt(i);
			if ((charAt >= 65) && (charAt <= 91)) {
				flag = false;
				break;
			} else {

			}
		}

		return flag;
	}

	public static String replaceLast(String str, String lastChars, String replaceBy) {

		String retSTR = "";
		String tempStr = str.trim() + " ";
		String templastChars = lastChars.trim() + " ";
		retSTR = tempStr.replaceAll(templastChars, replaceBy);
		return retSTR;
	}

	public static String trimFirstAndLastChar(String str) {

		if (isEmpty(str))
			return str;
		if (str.length() < 2)
			return str;
		StringBuffer sbf = new StringBuffer(str);

		sbf.deleteCharAt(sbf.length() - 1);
		sbf.deleteCharAt(0);
		return sbf.toString();
	}

	public static String trimFirstChar(String str) {

		if (isEmpty(str))
			return str;
		if (str.length() < 1)
			return str;
		StringBuffer sbf = new StringBuffer(str);
		sbf.deleteCharAt(0);

		return sbf.toString();
	}

	public static String trimLastChar(String str) {

		if (isEmpty(str))
			return str;
		if (str.length() < 1)
			return str;
		StringBuffer sbf = new StringBuffer(str);
		sbf.deleteCharAt(str.length() - 1);
		return sbf.toString();
	}

	public static boolean isEmpty(String str) {
		if (str == null)
			return true;
		if (str.trim().isEmpty())
			return true;

		return false;

	}

	public static String convertFirstCharToUpperCase(String str) {

		if (isNullOrEmpty(str))
			return str;
		if (str.length() == 1)
			return str.toUpperCase();

		String firstChar = "" + str.charAt(0);
		return firstChar.toUpperCase() + str.substring(1);

	}

	public static String convertFirstCharToLowerCase(String str) {

		if (isNullOrEmpty(str))
			return str;
		if (str.length() == 1)
			return str.toLowerCase();

		String firstChar = "" + str.charAt(0);
		return firstChar.toLowerCase() + str.substring(1);

	}

	public static int getWordCount(String str, String phrase) {
		int count = 0;

		int indx = str.indexOf(phrase);
		while (indx >= 0) {

			count++;
			indx = str.indexOf(phrase, indx + 1);
		}

		return count;
	}

	public static String getAsSingleLine(String str, boolean trimespace) {

		String xmlStr = str;
		if (xmlStr == null)
			xmlStr = "";
		if (xmlStr.isEmpty()) {
			return "";
		}

		String templateCodeInALine = "";
		StringTokenizer st = new StringTokenizer(xmlStr, "\n");
		while (st.hasMoreTokens()) {

			String line = st.nextToken();
			if (line == null)
				continue;

			if (trimespace) {
				line = line.trim();
			}

			line = line.replaceAll("\r", "");
			templateCodeInALine = templateCodeInALine + line;

		}

		return templateCodeInALine;
	}

	public static String convertToMultiLiine(String str, String delim) {

		StringTokenizer st = new StringTokenizer(str, delim);
		String content = "";
		while (st.hasMoreTokens()) {
			String token = st.nextToken();
			token = token.trim();

			token = token.replaceAll("\n", " ");

			if (token.isEmpty())
				continue;

			content = content + token + delim + "\n";
		}
		return content;
	}

	public static String replaceXMLReseredCharacter(String str) throws Exception {

		String content = str;

		content = content.replaceAll("\"", "&quot;");
		content = content.replaceAll("&", "&amp;");
		content = content.replaceAll("<", "&lt;");
		content = content.replaceAll(">", "&gt;");
		content = content.replaceAll("'", "&apos;");

		return content;

	}

}





Please provide your feedback here
It has been explained in this video (https://youtu.be/bxkNb0GuELY). Please watch it.
 


Post Your Comment:
Name :
Email ( Optional) :
Comments / Suggestion (* Required) It is required: :
: