org.apache.velocity.util

Class StringUtils


public class StringUtils
extends java.lang.Object

This class provides some methods for dynamically invoking methods in objects, and some string manipulation methods used by torque. The string methods will soon be moved into the turbine string utilities class.

Version:
$Id: StringUtils.java,v 1.16.8.1 2004/03/03 23:23:07 geirm Exp $

Authors:
Jason van Zyl
Daniel Rall

Field Summary

private static String
EOL
Line separator for the OS we are operating on.
private static int
EOL_LENGTH
Length of the line separator.

Method Summary

boolean
allEmpty(List list)
Check to see if all the string objects passed in are empty.
static String
capitalizeFirstLetter(String data)
Capitalize the first letter but leave the rest as they are.
static String
chop(String s, int i)
Chop i characters off the end of a string.
static String
chop(String s, int i, String eol)
Chop i characters off the end of a string.
static String
collapseNewlines(String argStr)
Remove/collapse multiple newline characters.
static String
collapseSpaces(String argStr)
Remove/collapse multiple spaces.
String
concat(List list)
Concatenates a list of objects as a String.
static String
fileContentsToString(String file)
Read the contents of a file and place them in a string object.
static String
firstLetterCaps(String data)
Makes the first letter caps and the rest lowercase.
static String
getPackageAsPath(String pckge)
Return a package name as a relative path name
static String
normalizePath(String path)
Return a context-relative path, beginning with a "/", that represents the canonical version of the specified path after ".." and "." elements are resolved out.
static String
removeAndHump(String data)
'Camels Hump' replacement of underscores.
static String
removeAndHump(String data, String replaceThis)
'Camels Hump' replacement.
static String
removeUnderScores(String data)
Deprecated. Use the org.apache.commons.util.StringUtils class instead.
String
select(boolean state, String trueString, String falseString)
If state is true then return the trueString, else return the falseString.
static String[]
split(String line, String delim)
Create a string array from a string separated by delim
static String
stackTrace(Throwable e)
Returns the output of printStackTrace as a String.
static StringBuffer
stringSubstitution(String argStr, Hashtable vars)
static StringBuffer
stringSubstitution(String argStr, Map vars)
Perform a series of substitutions.
static String
sub(String line, String oldString, String newString)
Replaces all instances of oldString with newString in line.

Field Details

EOL

private static final String EOL
Line separator for the OS we are operating on.


EOL_LENGTH

private static final int EOL_LENGTH
Length of the line separator.

Method Details

allEmpty

public boolean allEmpty(List list)
Check to see if all the string objects passed in are empty.

Parameters:
list - A list of java.lang.String objects.

Returns:
Whether all strings are empty.


capitalizeFirstLetter

public static String capitalizeFirstLetter(String data)
Capitalize the first letter but leave the rest as they are.

For example fooBar becomes FooBar.

Parameters:
data - capitalize this

Returns:
String


chop

public static String chop(String s,
                          int i)
Chop i characters off the end of a string. This method assumes that any EOL characters in String s and the platform EOL will be the same. A 2 character EOL will count as 1 character.

Parameters:
i - Number of characters to chop.

Returns:
String with processed answer.


chop

public static String chop(String s,
                          int i,
                          String eol)
Chop i characters off the end of a string. A 2 character EOL will count as 1 character.

Parameters:
i - Number of characters to chop.
eol - A String representing the EOL (end of line).

Returns:
String with processed answer.


collapseNewlines

public static String collapseNewlines(String argStr)
Remove/collapse multiple newline characters.

Parameters:

Returns:
String


collapseSpaces

public static String collapseSpaces(String argStr)
Remove/collapse multiple spaces.

Parameters:

Returns:
String


concat

public String concat(List list)
Concatenates a list of objects as a String.

Parameters:
list - The list of objects to concatenate.

Returns:
A text representation of the concatenated objects.


fileContentsToString

public static String fileContentsToString(String file)
Read the contents of a file and place them in a string object.

Parameters:

Returns:
String contents of the file.


firstLetterCaps

public static String firstLetterCaps(String data)
Makes the first letter caps and the rest lowercase.

For example fooBar becomes Foobar.

Parameters:
data - capitalize this

Returns:
String


getPackageAsPath

public static String getPackageAsPath(String pckge)
Return a package name as a relative path name

Parameters:

Returns:
String directory path.


normalizePath

public static final String normalizePath(String path)
Return a context-relative path, beginning with a "/", that represents the canonical version of the specified path after ".." and "." elements are resolved out. If the specified path attempts to go outside the boundaries of the current context (i.e. too many ".." path elements are present), return null instead.

Parameters:
path - Path to be normalized

Returns:
String normalized path


removeAndHump

public static String removeAndHump(String data)
'Camels Hump' replacement of underscores.

Remove underscores from a string but leave the capitalization of the other letters unchanged.

For example foo_barBar becomes FooBarBar.

Parameters:
data - string to hump

Returns:
String


removeAndHump

public static String removeAndHump(String data,
                                   String replaceThis)
'Camels Hump' replacement.

Remove one string from another string but leave the capitalization of the other letters unchanged.

For example, removing "_" from foo_barBar becomes FooBarBar.

Parameters:
data - string to hump
replaceThis - string to be replaced

Returns:
String


removeUnderScores

public static String removeUnderScores(String data)

Deprecated. Use the org.apache.commons.util.StringUtils class instead. Using its firstLetterCaps() method in conjunction with a StringTokenizer will achieve the same result.

Remove underscores from a string and replaces first letters with capitals. Other letters are changed to lower case.

For example foo_bar becomes FooBar but foo_barBar becomes FooBarbar.

Parameters:
data - string to remove underscores from.

Returns:
String


select

public String select(boolean state,
                     String trueString,
                     String falseString)
If state is true then return the trueString, else return the falseString.

Parameters:


split

public static String[] split(String line,
                             String delim)
Create a string array from a string separated by delim

Parameters:
line - the line to split
delim - the delimter to split by

Returns:
a string array of the split fields


stackTrace

public static final String stackTrace(Throwable e)
Returns the output of printStackTrace as a String.

Parameters:
e - A Throwable.

Returns:
A String.


stringSubstitution

public static StringBuffer stringSubstitution(String argStr,
                                              Hashtable vars)


stringSubstitution

public static StringBuffer stringSubstitution(String argStr,
                                              Map vars)
Perform a series of substitutions. The substitions are performed by replacing $variable in the target string with the value of provided by the key "variable" in the provided hashtable.

Parameters:

Returns:
String target string with replacements.


sub

public static final String sub(String line,
                               String oldString,
                               String newString)
Replaces all instances of oldString with newString in line. Taken from the Jive forum package.

Parameters:

Returns:
String string with replacements.


Copyright B) 2002 Apache Software Foundation. All Rights Reserved.