/*
 *  Copyright 2011 Eric F. Savage, code@efsavage.com
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.ajah.util;

/**
 * Utilities for dealing with Strings.
 * 
 * @author Eric F. Savage <code@efsavage.com>
 */
public class StringUtils {

	/**
	 * Looks for null or empty strings.
	 * 
	 * @param string
	 *            String to be tested, may be null
	 * @return true if string is null or zero-length
	 */
	public static boolean isBlank(final String string) {
		return string == null || string.length() < 1 || string.trim().length() < 1;
	}

	/**
	 * Returns length of string, 0 if null.
	 * 
	 * @param string
	 *            String to be tested, may be null.
	 * @return length of string, 0 if null.
	 */
	public static int safeLength(final String string) {
		return string == null ? 0 : string.length();
	}

	/**
	 * Converts camelCase text to regular text. Example: "canOfSoda" converts to
	 * "can of soda". Source: <a href=
	 * "http://stackoverflow.com/questions/2559759/how-do-i-convert-camelcase-into-human-readable-names-in-java"
	 * >Stack Overflow</a>
	 * 
	 * @param string
	 * @return String, de-camelcased.
	 */
	public static String splitCamelCase(final String string) {
		return string.replaceAll(String.format("%s|%s|%s", "(?<=[A-Z])(?=[A-Z][a-z])", "(?<=[^A-Z])(?=[A-Z])", "(?<=[A-Za-z])(?=[^A-Za-z])"), " ");
	}

	/**
	 * Capitalizes first letter of a String.
	 * 
	 * @see Character#toTitleCase(char)
	 * @param string
	 *            String to capitalized
	 * @return String, capitalized, may be null if null is passed in.
	 */
	public static String capitalize(final String string) {
		if (StringUtils.isBlank(string)) {
			return string;
		}
		return Character.toTitleCase(string.charAt(0)) + string.substring(1);
	}

	/**
	 * Returns toString() for the object passed, if it is not null, otherwise
	 * returns null.
	 * 
	 * @see Object#toString()
	 * @param object
	 *            The object to check/toString()
	 * @return object.toString() or null
	 */
	public static String safeToString(final Object object) {
		if (object == null) {
			return null;
		}
		return object.toString();
	}

	/**
	 * Joins an array of Strings into a single String with the specified
	 * delimeter.
	 * 
	 * @param array
	 *            Array of Strings, may be empty or null.
	 * @param delimiter
	 *            The delimiter to put between the joined Strings, may be empty
	 *            or null.
	 * @return The joined array with delimiter in between joined Strings. If
	 *         array was null or empty or contained only null or empty Strings,
	 *         returns null.
	 */
	public static String join(final String[] array, final String delimiter) {
		if (array == null || array.length == 0) {
			return null;
		} else if (array.length == 1) {
			return array[0];
		}
		final StringBuffer buf = new StringBuffer();
		boolean first = delimiter != null;
		for (int i = 0; i < array.length; i++) {
			if (!StringUtils.isBlank(array[i])) {
				if (!first) {
					buf.append(delimiter);
				} else {
					first = false;
				}
				buf.append(array[i]);
			}
		}
		if (buf.length() > 0) {
			return buf.toString();
		}
		return null;
	}

	/**
	 * Calls {@link #join(String[], String)} with null for a delimiter. Array of
	 * Strings, may be empty or null.
	 * 
	 * @param array
	 *            The array to be joined, may be empty or null.
	 * @return The joined array. If array was null or empty or contained only
	 *         null or empty Strings, returns null.
	 */
	public static String join(final String[] array) {
		return join(array, null);
	}

	/**
	 * Checks a string with {@link String#endsWith(String)} against an array of
	 * candidate strings.
	 * 
	 * @param string
	 *            The string to check.
	 * @param suffixes
	 *            The array of suffixes to use.
	 * @return true if the string ends with any of the suffixes, otherwise
	 *         false.
	 */
	public static boolean endsWith(String string, String[] suffixes) {
		if (isBlank(string) || suffixes == null || suffixes.length == 0) {
			return false;
		}
		for (String suffix : suffixes) {
			if (string.endsWith(suffix)) {
				return true;
			}
		}
		return false;
	}

}

@@ -4688,10 +4688,699 @@
 se;%0A%09%7D%0A%0A
+%09/**%0A%09 * Truncates a string if it is longer than desired.%0A%09 * %0A%09 * @param string%0A%09 *            The string to truncate.%0A%09 * @param maxLength%0A%09 *            The maximum length of the string, must be greater than 1.%0A%09 * @return The string truncated to maxLength characters if necessary,%0A%09 *         otherwise will return the original string, including null.%0A%09 */%0A%09public static String truncate(String string, int maxLength) %7B%0A%09%09if (StringUtils.isBlank(string)) %7B%0A%09%09%09return string;%0A%09%09%7D%0A%09%09if (maxLength %3C 1) %7B%0A%09%09%09throw new IllegalArgumentException(%22maxLength must be greater than 0%22);%0A%09%09%7D%0A%09%09if (string.length() %3E maxLength) %7B%0A%09%09%09return string.substring(0, maxLength);%0A%09%09%7D%0A%09%09return string;%0A%09%7D%0A%0A
 %7D%0A

Add truncate method