StringTools

Die Klasse StringTools stellt statische Hilfsmethoden Methoden zur Verfügung, welche immer wieder benötigt werden. So z.B. das Parsen von nummerischen und bolschen Werten, das zusammenschneiden von Zeichenketten, usw..

StringTools.java


package de.snowbits.tools;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;

public class StringTools {
	
	public enum CutString {
		BIGIN, END;
	}
	
	private StringTools() { }
	
	public static final String getStackTrace(Throwable e){
		StringWriter errors = new StringWriter();
		e.printStackTrace(new PrintWriter(errors));
		
		return errors.toString();
	}	
		
	/**
	 * @param parse
	 * @param errorValue
	 * @return Integer from parsed string, else if error then errorValue 
	 */
	public static final int parseInt(String parse, int errorValue) {
		return (Integer) parseNumber(parse, errorValue);
	}

	/**
	 * @param parse
	 * @param errorValue
	 * @return Long from parsed string, else if error then errorValue
	 */
	public static final long parseLong(String parse, long errorValue) {
		return (Long) parseNumber(parse, errorValue);
	}

	/**
	 * @param parse
	 * @param errorValue
	 * @return Float from parsed string, else if error then errorValue
	 */
	public static final float parseFloat(String parse, float errorValue) {
		return (Float) parseNumber(parse, errorValue);
	}
	
	/**
	 * @param parse
	 * @param errorValue
	 * @return from parsed string, else if error then errorValue
	 */
	public static final double parseDouble(String parse, double errorValue) {
		return (Double) parseNumber(parse, errorValue);
	}
	
	public static final <E extends Number> Number parseNumber(String parse, E errorValue) {
		try {
			if (errorValue instanceof Integer)
				return Integer.parseInt(parse);
			
			if (errorValue instanceof Long)
				return Long.parseLong(parse);
			
			if (errorValue instanceof Float)
				return Float.parseFloat(parse);
			
			if (errorValue instanceof Double)
				return Double.parseDouble(parse);
			
			throw new IllegalArgumentException("This type is not supported: " + errorValue.getClass());
			
		} catch (NumberFormatException e) {
			return errorValue;
		}
	}
		
	/**
	 * <pre>
	 * Example:
	 * 
	 * if (value.length() > maxLength)
	 *    return value...
	 * </pre>
	 * 
	 * @param value
	 * @param maxLength
	 * @return
	 */
	public static final String cutToValidString(String value, int maxLength) {
		
		return cutToValidString(value, maxLength, CutString.END);
	}
	
	/**
	 * <pre>
	 * Example:
	 * 
	 * if (value.length() > maxLength) {
	 * 	if (cutString == {@link CutString#END})
	 * 	  return value...
	 * 	else
	 * 	  return ...value // = {@link CutString#BIGIN}
	 * }
	 * </pre>
	 * 
	 * @param value
	 * @param maxLength
	 * @param cutString
	 * @return
	 */
	public static final String cutToValidString(String value, int maxLength, CutString cutString) {
		if (value == null)
			return value;
		
		if (value.length() > maxLength) {
			if (cutString == CutString.END)
				return value.substring(0, maxLength - 3) + "...";
			else
				return "..." + value.substring(value.length() - maxLength + 3, value.length());
		}
		
		return value;
	}
		
	/**
	 * Fill a number with string. <br />
	 * Supported types: {@link Integer}, {@link Long}, {@link Float}, {@link Double}
	 * <pre>
	 * format(200, 100, "#")          = 200
	 * format(1000, 100, "#"))        = 1000
	 * format(99, 100, "#"))          = #99
	 * format(99, 10000, "#"))        = ###99
	 * format(22l, 100, "#"))         = #22
	 * format(33.65f, 100, "#"))      = #33.65
	 * format(5555f, 100, "#"))       = 5555.0
	 * format(66.66d, 100, "#"))      = #66.66
	 * format(666.666666d, 100, "#")) = 666.666666
	 * </pre>
	 * 
	 * @param number
	 * @param length
	 * @param fill
	 * @return formated string
	 */
	public static final <T extends Number> String format(T number, int length, String fill) {
		int lenNumber = 0;

		if (number instanceof Integer || number instanceof Long) {
			lenNumber = String.valueOf(number).length();
		} else if (number instanceof Double || number instanceof Float) {
			String tmp = String.valueOf(number);
			int pos = tmp.indexOf(".");
			if (pos != -1)
				lenNumber = tmp.substring(0, pos).length() ;
			else
				lenNumber = tmp.length();
		} else {
			throw new IllegalArgumentException("This type is not supported: " + number.getClass().getName());
		}
		
		int diff = String.valueOf(length).length() - lenNumber;
		
		if (diff < 1)
			return String.valueOf(number);
		
		StringBuilder sb = new StringBuilder();
		
		for (int i = 0; i < diff; i++) 
			sb.append(fill);

		return sb.toString() + String.valueOf(number);
	}

	public static String trimLeft(String string) {
		int len = string.length();
		int index = 0;
		char[] chars = string.toCharArray();

		while ((index < len) && (chars[index] <= ' ')) {
			++index;
		}

		return (index > 0) ? string.substring(index, len) : string;
	}	
	
	public static String trimRight(String string) {
		int len = string.length();
		char[] chars = string.toCharArray();

		while ((len > 0) && (chars[len - 1] <= ' ')) {
			--len;
		}

		return (len < string.length()) ? string.substring(0, len) : string;
	}	
	
	/**
	 * <pre>
	 * Example:
	 * value = 12 then return 12
	 * value =  2 then return 02
	 * </pre>
	 * 
	 * @param value
	 * @return
	 */
	public static final String lessThanTen(int value) {
		return (value < 10 ? "0" + String.valueOf(value) : String.valueOf(value));
	}
	
	/**
	 * @param parse
	 * @return if parse = NULL, then return empty string
	 */
	public static final String parseNull(String parse) {
		
		return parse == null ? "" : parse;
	}	
	
	/**
	 * @param parse allowed false, true, 0, 1 ()
	 * @return
	 */
	public static final boolean parseBoolean(String parse) {
		parse = parse.trim();

		if (parse.length() < 1)
			return false;
		else if (parse.equalsIgnoreCase("false"))
			return false;
		else if (parse.equals("0"))
			return false;
		else if (parse.equalsIgnoreCase("true"))
			return true;
		else if (parse.equals("1"))
			return true;
		else
			throw new IllegalArgumentException("Error illegal argument for string parse=" + parse +
					"\nExplain:" +
					"\nif parse = empty string then return false" +
					"\nif parse = FALSE then return false" +
					"\nif parse = 0 then return false" +
					"\nif parse = TRUE then return true" +
					"\nif parse = 1 then return true");
	}
	
	/**
	 * @see #parseBoolean(String)
	 * @param parse
	 * @param defaultValue return value if parsing failed
	 * @return
	 */
	public static final boolean parseBoolean(String parse, boolean defaultValue) {
		try {
			return parseBoolean(parse);
		} catch (IllegalArgumentException e) {
			return defaultValue;
		}
	}	
	
	/**
	 * @param list
	 * @param emtyValue
	 * @return comma separated list
	 */
	public static final <T, E> String arrayToString(List<T> list, E emtyValue) {
		if (list.isEmpty())
			return String.valueOf(emtyValue);
		
		String value = Arrays.toString(list.toArray()).replace("[", "");
		return value.replace("]", "");
	}	
	
}
	

Top


Example
Sitemap Kontakt Impressum