FileDocCategorySizeDatePackage
StringHelper.javaAPI DocGlassfish v2 API11566Fri May 04 22:35:20 BST 2007com.sun.jdo.spi.persistence.utility

StringHelper

public class StringHelper extends Object
NOTE: These utilities have been moved from another (more specific package's) utility class so that more classes can have access to them. There can be some refactoring work to combine these with some of the methods in the StringScanner class.

Fields Summary
private static final char
BACKSLASH
constants for escape
private static final char
QUOTE
Constructors Summary
Methods Summary
public static java.lang.StringarrayToSeparatedList(java.util.List list, int beginIndex, int endIndex, java.lang.String separator)
Convert an array of objects into a separated string. This method assumes there is no instance of the separator in the strings of list.

param
list The list of objects to be expanded.
param
beginIndex The index of the first element in the list to be used.
param
endIndex The index of the last element in the list to be used.
param
separator The separator to be used between strings.
return
a string representing the expanded list.


	                                                                               	 
	       
		   
	
		StringBuffer result = new StringBuffer();

		if (list != null)
		{
			int i, count = (endIndex + 1);

			if ((count > beginIndex) && (list.size() >= count))
				result.append(list.get(beginIndex));

			for (i = beginIndex + 1; i < count; i++)
				result.append(separator + list.get(i));
		}

		return result.toString();
	
public static java.lang.StringarrayToSeparatedList(java.util.List list, int beginIndex, int endIndex)
Convert an array of objects into a separated string using the default separator. This method assumes there is no instance of the separator in the strings of list.

param
list The list of objects to be expanded.
param
beginIndex The index of the first element in the list to be used.
param
endIndex The index of the last element in the list to be used.
return
a string representing the expanded list.

		return arrayToSeparatedList(list, beginIndex, endIndex, ","); // NOI18N
	
public static java.lang.StringarrayToSeparatedList(java.util.List list, java.lang.String separator)
Convert an array of objects into a separated string using the specified separator and the entire array. This method assumes there is no instance of the separator in the strings of list.

param
list The list of objects to be expanded.
param
separator The separator to be used between strings.
return
a string representing the expanded list.

		return arrayToSeparatedList(list, 0, 
			((list != null) ? (list.size() - 1) : 0), separator);
	
public static java.lang.StringarrayToSeparatedList(java.util.List list)
Convert an array of objects into a separated string using the default separator and the entire array. This method assumes there is no instance of the separator in the strings of list.

param
list The list of objects to be expanded.
return
a string representing the expanded list.

		return arrayToSeparatedList(list, 0, 
			((list != null) ? (list.size() - 1) : 0));
	
public static java.lang.Stringescape(java.lang.String str)
Escaping given string by " and \.

param
str String to be escaped
return
string escaped by " and \.

		if (str == null)
		{
			return str;
		}
		else
		{
			int indS = str.indexOf(BACKSLASH);
			int indQ = str.indexOf(QUOTE);
			if (indS == -1 && indQ == -1)
			{
				return str;
			}
			else
			{
				StringBuffer buf = new StringBuffer();
				char data[] = str.toCharArray();
				for (int i = 0; i < data.length; i++)
				{
					if (BACKSLASH == data[i] || QUOTE == data[i])
					{
						buf.append(BACKSLASH);
					}
					buf.append(data[i]);
				}
				return buf.toString();
			}
		}
	
public static java.lang.StringgetCapitalizedString(java.lang.String aString)
Gets a version of the specified string with the first letter capitalized. This can be used to convert a field name to get and set method names.

param
aString the string to be capitalized
return
a capitalized for the specified string

		if (isEmpty(aString))
			return aString;

		return Character.toUpperCase(aString.charAt(0)) + aString.substring(1);
	
public static java.lang.StringintArrayToSeparatedList(int[] intArray, java.lang.String separator)
Convert an array of int values into a separated string.

param
intArray The array of int values to be expanded.
param
separator The separator to be used between strings.
return
a string representing the expanded array.

		return intArrayToSeparatedList(intArray, 0, 
			((intArray != null) ? (intArray.length - 1) : 0), separator);
	
public static java.lang.StringintArrayToSeparatedList(int[] intArray, int beginIndex, int endIndex, java.lang.String separator)
Convert an array of int values into a separated string.

param
intArray The array of int values to be expanded.
param
beginIndex The index of the first element in the array to be used.
param
endIndex The index of the last element in the array to be used.
param
separator The separator to be used between strings.
return
a string representing the expanded array.

		StringBuffer result = new StringBuffer();

		if (intArray != null) 
		{
			int count = (endIndex + 1);
			if ((count > beginIndex) && (intArray.length >= count))
				result.append(intArray[beginIndex]);

			for (int i = beginIndex + 1; i < count; i++) {
				result.append(separator);
				result.append(intArray[i]);
			}
		}

		return result.toString();
	
public static booleanisEmpty(java.lang.String aString)
Checks if a string is null or empty.

return
true if the string is null or empty after trim, false otherwirse.

		return ((aString == null) || (aString.trim().length() == 0));
	
public static java.lang.Stringreplace(java.lang.String string, java.lang.String oldString, java.lang.String newString)
Replaces all occurences of oldString in string with newString. The methods returns either a new string instance (in the case oldString is included in the string) or the origial string itself (in the case oldString is not included).

param
string the original string.
param
oldString the string to be replaced.
param
newString the string the old value is replaced with.
return
a string derived from the specified this string by replacing every occurrence of oldString with newString.

		StringBuffer sb = null;
		final int l = oldString.length();
		int	beginIndex = 0;
		int	index;

		while ((index = string.indexOf(oldString, beginIndex)) > -1)
		{
			// only create the StringBuffer if there's an occurence of oldString
			if (sb == null)
			{
				sb = new StringBuffer(string.length());
			}
			sb.append(string.substring(beginIndex, index));
			sb.append(newString);
			beginIndex = index + l;
		}

		// append the rest if the old value was found at least once
		if (sb != null)
		{
			sb.append(string.substring(beginIndex));
		}

		return (sb != null ? sb.toString() : string);	
	
public static java.lang.StringreplaceFirst(java.lang.String string, java.lang.String oldString, java.lang.String newString)
Replaces the first occurence of oldString in string with newString. The methods returns either a new string instance (in the case oldString is included in the string) or the origial string itself (in the case oldString is not included).

param
string the original string.
param
oldString the string to be replaced.
param
newString the string the old value is replaced with.
return
a string derived from the specified this string by replacing the first occurence oldString with newString.

		int index = string.indexOf(oldString);
		if (index != -1) {
			StringBuffer sb = new StringBuffer(string.length());
			sb.append(string.substring(0, index));
			sb.append(newString);
			sb.append(string.substring(index + oldString.length()));
			return sb.toString();
		}
		return string;
	
public static java.lang.Stringrtrim(java.lang.String input)
Trims trailing spaces from input.

param
input The input string.
return
A new string with trailing spaces trimmed. If there are no trailing spaces, returns input.

		String retVal = input;

		if (input != null)
		{
			int lastCharIndex = input.length() - 1;
			int originalLastCharIndex = lastCharIndex;

			while ((lastCharIndex >= 0) && 
				Character.isSpaceChar(input.charAt(lastCharIndex)))
			{
				lastCharIndex--;
			}
			if (lastCharIndex != originalLastCharIndex)
			{
				//We have characters to trim. 
				retVal = input.substring(0,lastCharIndex + 1);
			}
		}

		return retVal;
	
public static java.util.ListseparatedListToArray(java.lang.String list, java.lang.String separator)
Convert a separated string to an array of strings

param
list The string representing the list of objects.
param
separator The separator to be used to tokenize strings.
return
an array representing the tokenized list.

		ArrayList result = new ArrayList();

		if (list != null)
		{
			StringTokenizer st = new StringTokenizer(list, separator);
			int i, size = st.countTokens();

			for (i = 0; i < size; i++)
				result.add(st.nextToken());
		}

		return result;
	
public static java.util.ListseparatedListToArray(java.lang.String list)
Convert a separated string to an array of strings using the default separator.

param
list The string representing the list of objects.
return
an array representing the tokenized list.

		return separatedListToArray(list, ","); // NOI18N