Top Description Fields Constructors Methods
org.apache.tools.ant.util

public final Class StringUtils

extends Object
Class Inheritance
Imports
java.io.PrintWriter, .StringWriter, java.util.Arrays, .Collection, .Vector, java.util.stream.Collector, .Collectors, org.apache.tools.ant.BuildException

A set of helper methods related to string manipulation.

Field Summary

Modifier and TypeField and Description
private static final long
private static final long
public static final String
LINE_SEP

the line separator for this OS

private static final long
private static final long
private static final long

Constructor Summary

AccessConstructor and Description
private
StringUtils()

constructor to stop anyone instantiating the class

Method Summary

Modifier and TypeMethod and Description
public static boolean

Returns:

true if the character sequence represented by the argument is a suffix of the character sequence represented by the StringBuffer object; false otherwise. Note that the result will be true if the argument is the empty string.
endsWith
(StringBuffer
the buffer to perform the check on
buffer
,
String
the suffix
suffix
)

Checks that a string buffer ends up with a given string.

public static String

Returns:

the stacktrace from the given exception.
getStackTrace
(Throwable
the exception to get the stacktrace from.
t
)

Convenient method to retrieve the full stacktrace from a given exception.

public static String

Returns:

the joined string
join
(Collection<?>
Collection of the data to be joined (may be null)
collection
,
CharSequence
Separator between elements (may be null)
separator
)

Joins the string representation of the elements of a collection to a joined string with a given separator.

public static String

Returns:

the joined string
join
(Object[]
Array of the data to be joined (may be null)
array
,
CharSequence
Separator between elements (may be null)
separator
)

Joins the string representation of the elements of an array to a joined string with a given separator.

private static Collector<CharSequence, ?, String>
joining(CharSequence separator)

public static Vector<String>

Returns:

the list of lines available in the string.
lineSplit
(String
the string to split up into lines.
data
)

Splits up a string into a list of lines.

public static long

Returns:

a long value representation
parseHumanSizes
(String
the amount as a human readable string.
humanSize
)

Takes a human readable size representation eg 10K a long value.

public static String

Returns:

the string with the prefix
removePrefix
(String
String for check
string
,
String
Prefix to remove
prefix
)

Removes the prefix from a given string, if the string contains that prefix.

public static String

Returns:

the string with the suffix
removeSuffix
(String
String for check
string
,
String
Suffix to remove
suffix
)

Removes the suffix from a given string, if the string contains that suffix.

public static String

Returns:

the new string with replaced occurrences.
replace
(String
the string to replace occurrences into
data
,
String
the occurrence to replace.
from
,
String
the occurrence to be used as a replacement.
to
)

Replace occurrences into a string.
public static String

Returns:

converted string
resolveBackSlash
(String
raw string with possible embedded '\'s
input
)

xml does not do "c" like interpretation of strings.

public static Vector<String>

Returns:

the list of elements.
split
(String
the string to split up.
data
,
int
the separator character.
ch
)

Splits up a string where elements are separated by a specific character and return all elements.

public static String

Returns:

null if the input string is null or empty or contain only empty spaces. It returns the input string without leading and trailing spaces otherwise.
trimToNull
(String
String to trim
inputString
)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

GIGABYTEback to summary
private static final long GIGABYTE
KILOBYTEback to summary
private static final long KILOBYTE
LINE_SEPback to summary
public static final String LINE_SEP

Deprecated

the line separator for this OS

Annotations
@Deprecated
MEGABYTEback to summary
private static final long MEGABYTE
PETABYTEback to summary
private static final long PETABYTE
TERABYTEback to summary
private static final long TERABYTE

Constructor Detail

StringUtilsback to summary
private StringUtils()

constructor to stop anyone instantiating the class

Method Detail

endsWithback to summary
public static boolean endsWith(StringBuffer buffer, String suffix)

Checks that a string buffer ends up with a given string. It may sound trivial with the existing JDK API but the various implementation among JDKs can make those methods extremely resource intensive and perform poorly due to massive memory allocation and copying. See

Parameters
buffer:StringBuffer

the buffer to perform the check on

suffix:String

the suffix

Returns:boolean

true if the character sequence represented by the argument is a suffix of the character sequence represented by the StringBuffer object; false otherwise. Note that the result will be true if the argument is the empty string.

getStackTraceback to summary
public static String getStackTrace(Throwable t)

Convenient method to retrieve the full stacktrace from a given exception.

Parameters
t:Throwable

the exception to get the stacktrace from.

Returns:String

the stacktrace from the given exception.

joinback to summary
public static String join(Collection<?> collection, CharSequence separator)

Joins the string representation of the elements of a collection to a joined string with a given separator.

Parameters
collection:Collection<?>

Collection of the data to be joined (may be null)

separator:CharSequence

Separator between elements (may be null)

Returns:String

the joined string

joinback to summary
public static String join(Object[] array, CharSequence separator)

Joins the string representation of the elements of an array to a joined string with a given separator.

Parameters
array:Object[]

Array of the data to be joined (may be null)

separator:CharSequence

Separator between elements (may be null)

Returns:String

the joined string

joiningback to summary
private static Collector<CharSequence, ?, String> joining(CharSequence separator)
lineSplitback to summary
public static Vector<String> lineSplit(String data)

Splits up a string into a list of lines. It is equivalent to split(data, '\n').

Parameters
data:String

the string to split up into lines.

Returns:Vector<String>

the list of lines available in the string.

parseHumanSizesback to summary
public static long parseHumanSizes(String humanSize) throws Exception

Takes a human readable size representation eg 10K a long value. Doesn't support 1.1K or other rational values.

Parameters
humanSize:String

the amount as a human readable string.

Returns:long

a long value representation

Exceptions
Exception:
if there is a problem.
Since
Ant 1.7
removePrefixback to summary
public static String removePrefix(String string, String prefix)

Removes the prefix from a given string, if the string contains that prefix.

Parameters
string:String

String for check

prefix:String

Prefix to remove

Returns:String

the string with the prefix

removeSuffixback to summary
public static String removeSuffix(String string, String suffix)

Removes the suffix from a given string, if the string contains that suffix.

Parameters
string:String

String for check

suffix:String

Suffix to remove

Returns:String

the string with the suffix

replaceback to summary
public static String replace(String data, String from, String to)

Replace occurrences into a string.

Parameters
data:String

the string to replace occurrences into

from:String

the occurrence to replace.

to:String

the occurrence to be used as a replacement.

Returns:String

the new string with replaced occurrences.

Annotations
@Deprecated
resolveBackSlashback to summary
public static String resolveBackSlash(String input)

xml does not do "c" like interpretation of strings. i.e. \n\r\t etc. this method processes \n, \r, \t, \f, \\ also subs \s -> " \n\r\t\f" a trailing '\' will be ignored

Parameters
input:String

raw string with possible embedded '\'s

Returns:String

converted string

Since
Ant 1.7
splitback to summary
public static Vector<String> split(String data, int ch)

Splits up a string where elements are separated by a specific character and return all elements.

Parameters
data:String

the string to split up.

ch:int

the separator character.

Returns:Vector<String>

the list of elements.

trimToNullback to summary
public static String trimToNull(String inputString)
Parameters
inputString:String

String to trim

Returns:String

null if the input string is null or empty or contain only empty spaces. It returns the input string without leading and trailing spaces otherwise.