FileDocCategorySizeDatePackage
StringUtils.javaAPI DocApache Ant 1.707868Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.util

StringUtils

public final class StringUtils extends Object
A set of helper methods related to string manipulation.

Fields Summary
public static final String
LINE_SEP
the line separator for this OS
Constructors Summary
private StringUtils()
constructor to stop anyone instantiating the class

    
Methods Summary
public static booleanendsWith(java.lang.StringBuffer buffer, java.lang.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

param
buffer the buffer to perform the check on
param
suffix the suffix
return
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.

        if (suffix.length() > buffer.length()) {
            return false;
        }
        // this loop is done on purpose to avoid memory allocation performance
        // problems on various JDKs
        // StringBuffer.lastIndexOf() was introduced in jdk 1.4 and
        // implementation is ok though does allocation/copying
        // StringBuffer.toString().endsWith() does massive memory
        // allocation/copying on JDK 1.5
        // See http://issues.apache.org/bugzilla/show_bug.cgi?id=37169
        int endIndex = suffix.length() - 1;
        int bufferIndex = buffer.length() - 1;
        while (endIndex >= 0) {
            if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) {
                return false;
            }
            bufferIndex--;
            endIndex--;
        }
        return true;
    
public static java.lang.StringgetStackTrace(java.lang.Throwable t)
Convenient method to retrieve the full stacktrace from a given exception.

param
t the exception to get the stacktrace from.
return
the stacktrace from the given exception.

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        t.printStackTrace(pw);
        pw.flush();
        pw.close();
        return sw.toString();
    
public static java.util.VectorlineSplit(java.lang.String data)
Splits up a string into a list of lines. It is equivalent to split(data, '\n').

param
data the string to split up into lines.
return
the list of lines available in the string.


                                          
         
        return split(data, '\n");
    
public static longparseHumanSizes(java.lang.String humanSize)
Takes a human readable size representation eg 10K a long value. Doesn't support 1.1K or other rational values.

param
humanSize
return
a long value representation
throws
Exception
since
Ant 1.7

    	final long KILOBYTE = 1024;
    	final long MEGABYTE = KILOBYTE * 1024;
    	final long GIGABYTE = MEGABYTE * 1024;
    	final long TERABYTE = GIGABYTE * 1024;
    	final long PETABYTE = TERABYTE * 1024;
        //last character isn't a digit
    	if(!Character.isDigit(humanSize.charAt(humanSize.length()-1))) {
    		char c = humanSize.charAt(humanSize.length()-1);
    		long value = Long.valueOf(humanSize.substring(0, humanSize.length()-1)).longValue();
    		switch (c) {
    			case 'K":
    				return value * KILOBYTE;
    			case 'M":
    				return value * MEGABYTE;
    			case 'G":
    				return value * GIGABYTE;
    			case 'T":
    				return value * TERABYTE;
    			case 'P":
    				return value * PETABYTE;
    			default:
    				return value;
    		}
    	} else {
    	    return Long.parseLong(humanSize);
    	}
    
public static java.lang.Stringreplace(java.lang.String data, java.lang.String from, java.lang.String to)
Replace occurrences into a string.

param
data the string to replace occurrences into
param
from the occurrence to replace.
param
to the occurrence to be used as a replacement.
return
the new string with replaced occurrences.

        StringBuffer buf = new StringBuffer(data.length());
        int pos = -1;
        int i = 0;
        while ((pos = data.indexOf(from, i)) != -1) {
            buf.append(data.substring(i, pos)).append(to);
            i = pos + from.length();
        }
        buf.append(data.substring(i));
        return buf.toString();
    
public static java.lang.StringresolveBackSlash(java.lang.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

param
input raw string with possible embedded '\'s
return
converted string
since
Ant 1.7

        StringBuffer b = new StringBuffer();
        boolean backSlashSeen = false;
        for (int i = 0; i < input.length(); ++i) {
            char c = input.charAt(i);
            if (!backSlashSeen) {
                if (c == '\\") {
                    backSlashSeen = true;
                } else {
                    b.append(c);
                }
            } else {
                switch (c) {
                    case '\\":
                        b.append((char) '\\");
                        break;
                    case 'n":
                        b.append((char) '\n");
                        break;
                    case 'r":
                        b.append((char) '\r");
                        break;
                    case 't":
                        b.append((char) '\t");
                        break;
                    case 'f":
                        b.append((char) '\f");
                        break;
                    case 's":
                        b.append(" \t\n\r\f");
                        break;
                    default:
                        b.append(c);
                }
                backSlashSeen = false;
            }
        }
        return b.toString();
    
public static java.util.Vectorsplit(java.lang.String data, int ch)
Splits up a string where elements are separated by a specific character and return all elements.

param
data the string to split up.
param
ch the separator character.
return
the list of elements.

        Vector elems = new Vector();
        int pos = -1;
        int i = 0;
        while ((pos = data.indexOf(ch, i)) != -1) {
            String elem = data.substring(i, pos);
            elems.addElement(elem);
            i = pos + 1;
        }
        elems.addElement(data.substring(i));
        return elems;