FileDocCategorySizeDatePackage
StringUtils.javaAPI DocApache Axis 1.416480Sat Apr 22 18:57:28 BST 2006org.apache.axis.utils

StringUtils

public class StringUtils extends Object

Fields Summary
public static final String[]
EMPTY_STRING_ARRAY
An empty immutable String array.
Constructors Summary
private StringUtils()

    
Methods Summary
public static java.lang.StringescapeNumericChar(java.lang.String str)
write the escaped version of a given string

param
str string to be encoded
return
a new escaped String, null if null string input

        if (str == null) {
            return null;
        }
        try {
            StringWriter writer = new StringWriter(str.length());
            escapeNumericChar(writer, str);
            return writer.toString();
        } catch (IOException ioe) {
            // this should never ever happen while writing to a StringWriter
            ioe.printStackTrace();
            return null;
        }
    
public static voidescapeNumericChar(java.io.Writer out, java.lang.String str)
write the escaped version of a given string

param
out writer to write this string to
param
str string to be encoded

        if (str == null) {
            return;
        }
        int length = str.length();
        char character;
        for (int i = 0; i < length; i++) {
            character = str.charAt( i );
            if (character > 0x7F) {
                out.write("&#x");
                out.write(Integer.toHexString(character).toUpperCase());
                out.write(";");
            } else {
                out.write(character);
            }
        }
    
private static intgetStripEnd(java.lang.String str, java.lang.String stripChars)

        int end;
        if (str == null || (end = str.length()) == 0) {
            return -1;
        }
        if (stripChars == null) {
            while ((end != 0) && Character.isWhitespace(str.charAt(end - 1))) {
                end--;
            }
        } else if (stripChars.length() == 0) {
            return end;
        } else {
            while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) {
                end--;
            }
        }
        return end;
    
private static intgetStripStart(java.lang.String str, java.lang.String stripChars)

        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return -1;
        }
        int start = 0;
        if (stripChars == null) {
            while ((start != strLen) && Character.isWhitespace(str.charAt(start))) {
                start++;
            }
        } else if (stripChars.length() == 0) {
            return start;
        } else {
            while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) != -1)) {
                start++;
            }
        }
        return start;
    
public static booleanisEmpty(java.lang.String str)

Checks if a String is empty ("") or null.

StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false

NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank().

param
str the String to check, may be null
return
true if the String is empty or null

        return (str == null || str.length() == 0);
    
public static java.lang.String[]split(java.lang.String str, char separatorChar)

Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated as one separator.

A null input String returns null.

StringUtils.split(null, *) = null
StringUtils.split("", *) = []
StringUtils.split("a.b.c", '.') = ["a", "b", "c"]
StringUtils.split("a..b.c", '.') = ["a", "b", "c"]
StringUtils.split("a:b:c", '.') = ["a:b:c"]
StringUtils.split("a\tb\nc", null) = ["a", "b", "c"]
StringUtils.split("a b c", ' ') = ["a", "b", "c"]

param
str the String to parse, may be null
param
separatorChar the character used as the delimiter, null splits on whitespace
return
an array of parsed Strings, null if null String input

        if (str == null) {
            return null;
        }
        int len = str.length();
        if (len == 0) {
            return EMPTY_STRING_ARRAY;
        }
        List list = new ArrayList();
        int i = 0, start = 0;
        boolean match = false;
        while (i < len) {
            if (str.charAt(i) == separatorChar) {
                if (match) {
                    list.add(str.substring(start, i));
                    match = false;
                }
                start = ++i;
                continue;
            }
            match = true;
            i++;
        }
        if (match) {
            list.add(str.substring(start, i));
        }
        return (String[]) list.toArray(new String[list.size()]);
    
public static booleanstartsWithIgnoreWhitespaces(java.lang.String prefix, java.lang.String string)
Tests if this string starts with the specified prefix (Ignoring whitespaces)

param
prefix
param
string
return
boolean

    
                            
           
        int index1 = 0;
        int index2 = 0;
        int length1 = prefix.length();
        int length2 = string.length();
        char ch1 = ' ";
        char ch2 = ' ";
        while (index1 < length1 && index2 < length2) {
            while (index1 < length1 && Character.isWhitespace(ch1 = prefix.charAt(index1))) {
                index1++;
            }
            while (index2 < length2 && Character.isWhitespace(ch2 = string.charAt(index2))) {
                index2++;
            }
            if (index1 == length1 && index2 == length2) {
                return true;
            }
            if (ch1 != ch2) {
                return false;
            }
            index1++;
            index2++;
        }
        if(index1 < length1 && index2 >= length2)
            return false;
        return true;
    
public static java.lang.Stringstrip(java.lang.String str)

Strips whitespace from the start and end of a String.

This removes whitespace. Whitespace is defined by {@link Character#isWhitespace(char)}.

A null input String returns null.

StringUtils.strip(null) = null
StringUtils.strip("") = ""
StringUtils.strip(" ") = ""
StringUtils.strip("abc") = "abc"
StringUtils.strip(" abc") = "abc"
StringUtils.strip("abc ") = "abc"
StringUtils.strip(" abc ") = "abc"
StringUtils.strip(" ab c ") = "ab c"

param
str the String to remove whitespace from, may be null
return
the stripped String, null if null String input

        return strip(str, null);
    
public static java.lang.Stringstrip(java.lang.String str, java.lang.String stripChars)

Strips any of a set of characters from the start and end of a String. This is similar to {@link String#trim()} but allows the characters to be stripped to be controlled.

A null input String returns null. An empty string ("") input returns the empty string.

If the stripChars String is null, whitespace is stripped as defined by {@link Character#isWhitespace(char)}. Alternatively use {@link #strip(String)}.

StringUtils.strip(null, *) = null
StringUtils.strip("", *) = ""
StringUtils.strip("abc", null) = "abc"
StringUtils.strip(" abc", null) = "abc"
StringUtils.strip("abc ", null) = "abc"
StringUtils.strip(" abc ", null) = "abc"
StringUtils.strip(" abcyx", "xyz") = " abc"

param
str the String to remove characters from, may be null
param
stripChars the characters to remove, null treated as whitespace
return
the stripped String, null if null String input

        if (str == null) {
            return str;
        }
        int len = str.length();
        if (len == 0) {
            return str;
        }
        int start = getStripStart(str, stripChars);
        if (start == len) {
            return "";
        }
        int end = getStripEnd(str, stripChars);
        return (start == 0 && end == len) ? str : str.substring(start, end);
    
public static java.lang.StringstripEnd(java.lang.String str, java.lang.String stripChars)

Strips any of a set of characters from the end of a String.

A null input String returns null. An empty string ("") input returns the empty string.

If the stripChars String is null, whitespace is stripped as defined by {@link Character#isWhitespace(char)}.

StringUtils.stripEnd(null, *) = null
StringUtils.stripEnd("", *) = ""
StringUtils.stripEnd("abc", "") = "abc"
StringUtils.stripEnd("abc", null) = "abc"
StringUtils.stripEnd(" abc", null) = " abc"
StringUtils.stripEnd("abc ", null) = "abc"
StringUtils.stripEnd(" abc ", null) = " abc"
StringUtils.stripEnd(" abcyx", "xyz") = " abc"

param
str the String to remove characters from, may be null
param
stripChars the characters to remove, null treated as whitespace
return
the stripped String, null if null String input

        int end = getStripEnd(str, stripChars);
        return (end < 0) ? str : str.substring(0, end);
    
public static java.lang.StringstripStart(java.lang.String str, java.lang.String stripChars)

Strips any of a set of characters from the start of a String.

A null input String returns null. An empty string ("") input returns the empty string.

If the stripChars String is null, whitespace is stripped as defined by {@link Character#isWhitespace(char)}.

StringUtils.stripStart(null, *) = null
StringUtils.stripStart("", *) = ""
StringUtils.stripStart("abc", "") = "abc"
StringUtils.stripStart("abc", null) = "abc"
StringUtils.stripStart(" abc", null) = "abc"
StringUtils.stripStart("abc ", null) = "abc "
StringUtils.stripStart(" abc ", null) = "abc "
StringUtils.stripStart("yxabc ", "xyz") = "abc "

param
str the String to remove characters from, may be null
param
stripChars the characters to remove, null treated as whitespace
return
the stripped String, null if null String input

        int start = getStripStart(str, stripChars);
        return (start <= 0) ? str : str.substring(start);
    
public static java.lang.StringunescapeNumericChar(java.lang.String str)

Unescapes numeric character referencs found in the String.

For example, it will return a unicode string which means the specified numeric character references looks like "ようこそ".

param
str the String to unescape, may be null
return
a new unescaped String, null if null string input

        if (str == null) {
            return null;
        }
        try {
            StringWriter writer = new StringWriter(str.length());
            unescapeNumericChar(writer, str);
            return writer.toString();
        } catch (IOException ioe) {
            // this should never ever happen while writing to a StringWriter
            ioe.printStackTrace();
            return null;
        }
    
public static voidunescapeNumericChar(java.io.Writer out, java.lang.String str)

Unescapes numeric character references found in the String to a Writer.

For example, it will return a unicode string which means the specified numeric character references looks like "ようこそ".

A null string input has no effect.

param
out the Writer used to output unescaped characters
param
str the String to unescape, may be null
throws
IllegalArgumentException if the Writer is null
throws
java.io.IOException if error occurs on underlying Writer

        if (out == null) {
            throw new IllegalArgumentException("The Writer must not be null");
        }
        if (str == null) {
            return;
        }

        int sz = str.length();
        StringBuffer unicode = new StringBuffer(4);
        StringBuffer escapes = new StringBuffer(3);
        boolean inUnicode = false;

        for (int i = 0; i < sz; i++) {
            char ch = str.charAt(i);
            if (inUnicode) {
                // if in unicode, then we're reading unicode
                // values in somehow
                unicode.append(ch);
                if (unicode.length() == 4) {
                    // unicode now contains the four hex digits
                    // which represents our unicode character
                    try {
                        int value = Integer.parseInt(unicode.toString(), 16);
                        out.write((char) value);
                        unicode.setLength(0);
                        // need to skip the delimiter - ';'
                        i = i + 1;
                        inUnicode = false;
                    } catch (NumberFormatException nfe) {
                        throw new InternalException(nfe);
                    }
                }
                continue;
            } else if (ch=='&") {
                // Start of the escape sequence ...
                // At least, the numeric character references require 8 bytes to
                // describe a Unicode character like as"￿"
                if (i+7 <= sz) {
                    escapes.append(ch);
                    escapes.append(str.charAt(i+1));
                    escapes.append(str.charAt(i+2));
                    if (escapes.toString().equals("&#x") && str.charAt(i+7)==';") {
                        inUnicode = true;
                    } else {
                        out.write(escapes.toString());
                    }
                    escapes.setLength(0);
                    // need to skip the escaping chars - '&#x'
                    i = i + 2;
                } else {
                    out.write(ch);
                }
                continue;
            }
            out.write(ch);
        }