ariba.util.core
Class StringUtil

java.lang.Object
  extended by ariba.util.core.StringUtil

public final class StringUtil
extends Object

String Utilities. These are utilities for working with strings.


Nested Class Summary
static class StringUtil.SearchResult
          Small struct class that captures the result of search for a string within some other unidentified string or stream.
 
Method Summary
static String bit(int pint)
          Create the appropriate bit string for use in a LIKE clause for a string bit mask.
static String bitClear(String value, int pos)
          Bit Clear - clear the bit with the given position in the given string Passing pos = 0 and value = "101" would yield "100".
static String bitSet(String value, int pos)
          Bit Set - sets the bit with the given position in the given string Passing pos = 0 and value = "100" would yield "101".
static boolean bitTest(String value, int pos)
          Bit Test - test the bit for the given position in the given string Passing pos = 0 and value = "100" would yield false Passing pos = 1 and value = "100" would yield false Passing pos = 2 and value = "100" would yield true
static String capitalizeFirstChar(String string)
          Returns string with the first character upper-cased.
static boolean charsEqualString(char[] chars, String s)
          Compare a character array to a string without allocating a temporary string.
static String compressSpaces(String s)
          Replace all repeating instances of a space in a String with a single space.
static boolean contains(String string, String phrase)
          Case insensitive string search for a substring.
static String[] delimitedStringToArray(String str, char delimiter)
          Tokenize a String with a specified separator character and return an array.
static boolean equalsIgnoreCase(String one, String two)
          Compares if two strings are equal or not.
static String escapeRegEx(String regex)
          Escapes metacharacters in a regex into literals.
static String fastJoin(Collection collection, String joinString)
          Joins collection of elements with a string between each pair of elements.
static String fastJoin(List vector, String joinString)
          Joins vector of elements with a string between each pair of elements.
static byte[] getBytesUTF8(String string)
           
static String getStringUTF8(byte[] bytes)
           
static int indexOf(String string, String[] substrings)
          Search for any of an array of substrings inside of the specified string.
static String intern(String source)
           
static boolean is7bitAscii(char c)
          Specifies whether the given character is a 7-ascii character
static boolean isAllDigits(String string)
          Check if a String is composed entirely of digits using Character.isDigit
static boolean isJavaIdentifier(String identifier)
          Check if a string is a legal java identifier.
static boolean isValidURIPart(String string, boolean allowForExtendedMarks)
          Specifies whether the given String is a valid URI part.
static String join(List vector, String joinString)
          Joins vector of elements with a string between each pair of elements.
static String join(Object[] objects, String joinString)
          Joins array of elements with a string between each pair of elements.
static String join(Object[] objects, String joinString, int index, int length)
          Joins a sub-array of the given objects array elements with a string between each pair of elements.
static boolean nullOrEmptyOrBlankString(String string)
          Determine if a string is null or empty or entirely spaces.
static boolean nullOrEmptyString(String string)
          Determine if a String is null or empty.
static int occurs(String[] array, String string)
          Count the number of occurrences of a string in an array of Strings.
static int occurs(String str, char ch)
          Count the number of occurrences of a character in a string.
static int occurs(String str, String substr)
          Count the number of occurrences of a substring in a string.
static String[] parseMatchingParentheses(String str)
           
static String printSecret(String value)
           
static String removeCarriageReturns(String string)
          Removes the newlines and carriage returns from string and returns the resultant string.
static String[] removeDuplicates(String[] s)
          To remove duplicate elements from a String array s
static String[] removeDuplicates(String[] s, StringArray duplicateArray)
          To remove duplicate elements from a String array s
static String removeLeadingSlashIfAny(String path)
          Removes the leading slash from the given String.
static String removeTrailingSlashIfAny(String path)
          Removes the trailing slash from the given String.
static String replaceCharByChar(String string, char marker, char replace)
          Replace all occurrences of a character in a String with another character.
static String replaceCharByString(String string, char marker, String replace)
          Replace all occurrences of a character in a String with a String.
static StringUtil.SearchResult search(String toSearch, int fromIdx, Collection toFind)
          Searches for the first one of any of the supplied strings
static String separateAtCapitalization(String string)
           
static String separateAtCapitalization(String string, char separationChar)
           
static List splitWithQuotes(String str, boolean keepQuotes)
          Splits a string up into chunks using a whitespace delimiter.
static boolean startsWithIgnoreCase(String string, String other)
          Check if a string starts with a specific substring in a case insensitive manner.
static String strcat(String[] elems)
           
static String strcat(String s1, String s2)
           
static String strcat(String s1, String s2, String s3)
           
static String strcat(String s1, String s2, String s3, String s4)
           
static String strcat(String s1, String s2, String s3, String s4, String s5)
           
static String strcat(String s1, String s2, String s3, String s4, String s5, String s6)
           
static String strcat(String s1, String s2, String s3, String s4, String s5, String s6, String s7)
           
static String strcat(String s1, String s2, String s3, String s4, String s5, String s6, String s7, String s8)
           
static String strcat(String s1, String s2, String s3, String s4, String s5, String s6, String s7, String s8, String s9)
           
static int stringArrayIndex(String[] keys, String key)
          Performs a binary search on a sorted string array.
static boolean stringMatchesPattern(String str, String pattern)
          Checks if a string matches a perl 5 pattern.
static List stringToStringsListUsingBreakChars(String s, String breakChars)
          Tokenize a string separated by any separator characters into a List.
static String substring(String string, int beginIndex, int endIndex)
          Returns the substring of string from beginIndex (inclusive) to endIndex, exclusive.
static String trailing(String string, int length)
          Returns the trailing part of string up to a max of length characters.
static String truncate(String string, int length)
          Returns this string truncated to a max of length characters.
static int unorderedStringArrayIndex(String[] keys, String key)
          Search an unsorted array of strings.
static int unorderedStringArrayIndexIdentical(String[] keys, String key)
          Search an unsorted array of strings.
static String wrap(String string, String lineSeparator, int wrapLength)
          Return the string argument with the lineSeparator inserted every wrapLength- characters.
static String wrapHTML(String string, int wrapLength)
          Return the string argument with "
" tag inserted every wrapLength- characters.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

nullOrEmptyString

public static boolean nullOrEmptyString(String string)
Determine if a String is null or empty.

Parameters:
string - a String object to check
Returns:
true if string is null or empty, false otherwise

nullOrEmptyOrBlankString

public static boolean nullOrEmptyOrBlankString(String string)
Determine if a string is null or empty or entirely spaces.

Parameters:
string - a String object to check
Returns:
true if string is null, empty, or all spaces; false otherwise

capitalizeFirstChar

public static String capitalizeFirstChar(String string)
Returns string with the first character upper-cased. If string is already uppercased or if it is null or less than one in length, the passed-in value is returned.

Returns:
string with the first character upper-cased

separateAtCapitalization

public static String separateAtCapitalization(String string)

separateAtCapitalization

public static String separateAtCapitalization(String string,
                                              char separationChar)

stringArrayIndex

public static final int stringArrayIndex(String[] keys,
                                         String key)
Performs a binary search on a sorted string array.

Parameters:
keys - a sorted array of string keys to search
key - a key to search for
Returns:
the index of the key in keys, or -1 if it's not found.

intern

public static String intern(String source)

stringMatchesPattern

public static boolean stringMatchesPattern(String str,
                                           String pattern)
Checks if a string matches a perl 5 pattern. See: org.apache.oro.text.perl.Perl5Util.match() example: stringMatchesPattern("foo", "/f.o/") returns true.


unorderedStringArrayIndexIdentical

public static final int unorderedStringArrayIndexIdentical(String[] keys,
                                                           String key)
Search an unsorted array of strings.

Parameters:
keys - an array of string keys to search
key - a key to search for using the == operator
Returns:
the index of the key in keys, or -1 if it's not found.

unorderedStringArrayIndex

public static final int unorderedStringArrayIndex(String[] keys,
                                                  String key)
Search an unsorted array of strings.

Parameters:
keys - an array of string keys to search
key - a key to search for using the equals() method
Returns:
the index of the key in keys, or -1 if it's not found.

delimitedStringToArray

public static String[] delimitedStringToArray(String str,
                                              char delimiter)
Tokenize a String with a specified separator character and return an array.

Takes a string in the form "Description.Price.Amount" with a delimiter of '.' and turns it into an array of strings:

        path[0] = "Description"
        path[1] = "Price"
        path[2] = "Amount"
        

Parameters:
str - a string to tokenize
delimiter - the delimiter to use when tokenizing
Returns:
an array of Strings containing each substring but excluding the delimiter character
See Also:
ListUtil.delimitedStringToList(java.lang.String, char)

occurs

public static int occurs(String str,
                         char ch)
Count the number of occurrences of a character in a string.

Parameters:
str - the String to count in
ch - the character to count the occurrences of
Returns:
the number of times the character ch occurs in the string str.

occurs

public static int occurs(String str,
                         String substr)
Count the number of occurrences of a substring in a string.

Parameters:
str - the String to count in
substr - the substring to count the occurrences of
Returns:
the number of times the String substr occurs in the string str. Throws a FatalAssertionException if substr is an empty string.

occurs

public static int occurs(String[] array,
                         String string)
Count the number of occurrences of a string in an array of Strings.

Parameters:
array - an array of Strings to search in. There may not be null elements in the array.
string - the String to match against
Returns:
the number of times the String string occurs in the String array array.

contains

public static boolean contains(String string,
                               String phrase)
Case insensitive string search for a substring.

Parameters:
string - the String to search in
phrase - the String to search for
Returns:
true if phrase is found anywhere within string, ignoring case; false otherwise. Returns true if phrase is empty.

indexOf

public static int indexOf(String string,
                          String[] substrings)
Search for any of an array of substrings inside of the specified string.

Parameters:
string - the String to search in
substrings - the strings to search for
Returns:
the index of the first substring that is found inside the string or -1 if the string is not found.

replaceCharByChar

public static String replaceCharByChar(String string,
                                       char marker,
                                       char replace)
Replace all occurrences of a character in a String with another character. Replaces each occurrence of marker in the string string with the character replace.

Parameters:
string - the string to do do the replacing on. If null or empty string, the string is simply returned.
marker - the character to remove
replace - the character to replace the removed character marker with
Returns:
a string with all occurrences of marker replaced by replace

replaceCharByString

public static String replaceCharByString(String string,
                                         char marker,
                                         String replace)
Replace all occurrences of a character in a String with a String. Replaces each occurrence of marker in the string string with the String replace.

Parameters:
string - the string to do do the replacing on. If null or empty string, the string is simply returned.
marker - the character to remove
replace - the String that will replace the removed marker. If an empty string or null is passed, any occurrences of the marker are removed from the initial string.
Returns:
a string with all occurrences of marker replaced by replace

compressSpaces

public static String compressSpaces(String s)
Replace all repeating instances of a space in a String with a single space. Replaces each repeating occurrence of the space character in the string string with exactly one space character.

Parameters:
s - the string to be compressed
Returns:
a string with all repeating occurrences of the space character replaced with a single space

charsEqualString

public static boolean charsEqualString(char[] chars,
                                       String s)
Compare a character array to a string without allocating a temporary string.

Parameters:
chars - the characters to check against the String
s - the string to compare to the characters
Returns:
true if the char[] array chars would be equal to the String s were the char[] array to be a String; false is returned otherwise

stringToStringsListUsingBreakChars

public static List stringToStringsListUsingBreakChars(String s,
                                                      String breakChars)
Tokenize a string separated by any separator characters into a List. Breaks up the specified string s into lines, and returns all lines in a vector. Blank lines may be optionally ignored.

Parameters:
s - the String to tokenize
breakChars - a string containing all characters to use as separator characters for the tokenize
Returns:
a List of the substrings, excluding the breakChars.

substring

public static String substring(String string,
                               int beginIndex,
                               int endIndex)
Returns the substring of string from beginIndex (inclusive) to endIndex, exclusive.

This function is "safe" in the sense that it will bound beginIndex and endIndex appropriately so that no exceptions will be thrown. E.g. if beginIndex is less than zero, the zero index will be used for the substring instead.

Parameters:
string - the string to substring
beginIndex - the beginning index, inclusive
endIndex - the end index, exclusive
Returns:
the substring'ed string or null if string is null

truncate

public static String truncate(String string,
                              int length)
Returns this string truncated to a max of length characters.

Parameters:
string - the string to truncate
length - the length of the truncated string
Returns:
the truncated string or null if string is null

trailing

public static String trailing(String string,
                              int length)
Returns the trailing part of string up to a max of length characters.

Parameters:
string - the string to get the trailing part of
length - the length of the truncated string
Returns:
the truncated string or null if string is null

join

public static String join(Object[] objects,
                          String joinString)
Joins array of elements with a string between each pair of elements.

Parameters:
objects - an array of Objects each which will be toStringed.
joinString - a string to separate each pair of elements, if null the empty string is assumed
Returns:
a string, created by joining the contents of the object array, separated by the joinString

join

public static String join(Object[] objects,
                          String joinString,
                          int index,
                          int length)
                   throws IndexOutOfBoundsException
Joins a sub-array of the given objects array elements with a string between each pair of elements. The sub-array is specified by the index offset and the length.

Range checking is delegated to the native access provided when accessing array elements.

Parameters:
objects - an array of Objects each which will be toStringed.
joinString - a string to separate each pair of elements, if null the empty string is assumed
index - the index within the array at which to begin the join of the elements; should be >= 0 and <= objects.length. (Note that if index == objects.length, length must equal 0 in order that an exception will not be thrown.)
length - the number of elements within the array to join together; should satisfy index + length <= objects.length
Returns:
a string, created by joining the contents of the object sub-array, separated by the joinString
Throws:
IndexOutOfBoundsException - if bounds specified lead to access outside the bounds of the objects array

fastJoin

public static String fastJoin(List vector,
                              String joinString)
Joins vector of elements with a string between each pair of elements.

Parameters:
vector - a vector of Objects each which will be toStringed.
joinString - a string to separate each pair of elements.
Returns:
a string, created by joining the contents of the List, separated by the joinString

fastJoin

public static String fastJoin(Collection collection,
                              String joinString)
Joins collection of elements with a string between each pair of elements.

Parameters:
collection - a collection of Objects each which will be toStringed.
joinString - a string to separate each pair of elements.
Returns:
a string, created by joining the contents of the List, separated by the joinString

join

public static String join(List vector,
                          String joinString)
Joins vector of elements with a string between each pair of elements. This will invoke formatters to format each object.

Parameters:
vector - a vector of Objects each which will be formatted using the formatters.
joinString - a string to separate each pair of elements.
Returns:
a string, created by joining the contents of the vector, separated by the joinString. The vector contents are formatted by the Formatter class.

isJavaIdentifier

public static boolean isJavaIdentifier(String identifier)
Check if a string is a legal java identifier.

Parameters:
identifier - The string to check
Returns:
true if identifier is legal, false otherwise. If identifier is null or empty, it is considered invalid

startsWithIgnoreCase

public static boolean startsWithIgnoreCase(String string,
                                           String other)
Check if a string starts with a specific substring in a case insensitive manner. Similar to String.startsWith but ignores case.

Parameters:
string - the string to search in
other - the string to check for
Returns:
true if the first characters of string are equal to the string other in a case insensitive comparison; false otherwise. Returns true if other is an empty string.
See Also:
String.startsWith(java.lang.String, int), String.regionMatches(boolean, int, String, int, int)

isAllDigits

public static boolean isAllDigits(String string)
Check if a String is composed entirely of digits using Character.isDigit

Parameters:
string - the String to check
Returns:
true if string is made entirely up of digits or is empty, false otherwise.
See Also:
Character.isDigit(char)

bitSet

public static final String bitSet(String value,
                                  int pos)
Bit Set - sets the bit with the given position in the given string Passing pos = 0 and value = "100" would yield "101".


bitClear

public static final String bitClear(String value,
                                    int pos)
Bit Clear - clear the bit with the given position in the given string Passing pos = 0 and value = "101" would yield "100".


bitTest

public static final boolean bitTest(String value,
                                    int pos)
Bit Test - test the bit for the given position in the given string Passing pos = 0 and value = "100" would yield false Passing pos = 1 and value = "100" would yield false Passing pos = 2 and value = "100" would yield true


bit

public static final String bit(int pint)
Create the appropriate bit string for use in a LIKE clause for a string bit mask.


strcat

public static String strcat(String s1,
                            String s2)

strcat

public static String strcat(String s1,
                            String s2,
                            String s3)

strcat

public static String strcat(String s1,
                            String s2,
                            String s3,
                            String s4)

strcat

public static String strcat(String s1,
                            String s2,
                            String s3,
                            String s4,
                            String s5)

strcat

public static String strcat(String s1,
                            String s2,
                            String s3,
                            String s4,
                            String s5,
                            String s6)

strcat

public static String strcat(String s1,
                            String s2,
                            String s3,
                            String s4,
                            String s5,
                            String s6,
                            String s7)

strcat

public static String strcat(String s1,
                            String s2,
                            String s3,
                            String s4,
                            String s5,
                            String s6,
                            String s7,
                            String s8)

strcat

public static String strcat(String s1,
                            String s2,
                            String s3,
                            String s4,
                            String s5,
                            String s6,
                            String s7,
                            String s8,
                            String s9)

strcat

public static String strcat(String[] elems)

removeDuplicates

public static String[] removeDuplicates(String[] s)
To remove duplicate elements from a String array s

Parameters:
s - a String array to unique
Returns:
a String array of unique elements

removeCarriageReturns

public static String removeCarriageReturns(String string)
Removes the newlines and carriage returns from string and returns the resultant string.

Parameters:
string - the string to remove the carriage returns from
Returns:
the string with carriage returns removed

removeDuplicates

public static String[] removeDuplicates(String[] s,
                                        StringArray duplicateArray)
To remove duplicate elements from a String array s

Parameters:
s - a String array to unique
duplicateArray - a list of duplicate array elements found in String array s
Returns:
a String array of unique elements

removeTrailingSlashIfAny

public static String removeTrailingSlashIfAny(String path)
Removes the trailing slash from the given String. Note that only one trailing slash is removed. It is assumed that the given path contains at most one traling slash. Note that the slash is either forward or backward, based on the OS.

Parameters:
path - the given path String, must not be null.
Returns:
the string with the trailing slash removed, if any.

removeLeadingSlashIfAny

public static String removeLeadingSlashIfAny(String path)
Removes the leading slash from the given String. Note that only one leading slash is removed. It is assumed that the given path contains at most one leading slash. Note that the slash is either forward or backward, based on the OS.

Parameters:
path - the given path String, must not be null.
Returns:
the string with the leading slash removed, if any.

printSecret

public static String printSecret(String value)

is7bitAscii

public static boolean is7bitAscii(char c)
Specifies whether the given character is a 7-ascii character

Parameters:
c - the character to test
Returns:
true if the character c is a 7-ascii character

isValidURIPart

public static boolean isValidURIPart(String string,
                                     boolean allowForExtendedMarks)
Specifies whether the given String is a valid URI part. According to the RFC 2396, a URI part is made of any alpha-numerical characters as well as some authorized marks ( '-' '_' '.' '!' '~' '*' ''' '(' ')' ). This returns true if all the characters part of the String follow the RFC speficication.

However because some of these marks can be troublesome, this method will return false if they are part of the String and allowForExtendedMarks is false.

Parameters:
string - the String to verify.
allowForExtendedMarks - if false the marks '!', '~', '*', ''', '(' and ')' will not be considered valid characters (although the RFC 2396 allows them.
Returns:
true when the String complies with RFC 2396

splitWithQuotes

public static List splitWithQuotes(String str,
                                   boolean keepQuotes)
Splits a string up into chunks using a whitespace delimiter. Quoted items are treated as a single chunk.

Parameters:
str - the string to split
keepQuotes - if true keep the quotes that encapsulate an element
Returns:
a List of String containing each seperated element

equalsIgnoreCase

public static boolean equalsIgnoreCase(String one,
                                       String two)
Compares if two strings are equal or not. It is different from equalsIgnoreCase method of java.lang.String in the sense that it makes a null check of each of the Strings to be compared.

Parameters:
one - the String to be compared
two - the String to be compared
Returns:
true if both are null or equalsIgnoreCase of java.lang.String returns true. False if one of them are null or equalsIgnoreCase of java.lang.String returns false.

wrapHTML

public static String wrapHTML(String string,
                              int wrapLength)
Return the string argument with "
" tag inserted every wrapLength- characters. Doesn't break a word, correctly handles punctuation and hyphenated words.

Parameters:
string -
wrapLength -
Returns:
wrapped string

wrap

public static String wrap(String string,
                          String lineSeparator,
                          int wrapLength)
Return the string argument with the lineSeparator inserted every wrapLength- characters. Doesn't break a word, correctly handles punctuation and hyphenated words.
 StringUtil.wrap("Hello World!, null, 9) = Hello
World! StringUtil.wrap("1234512345", "
", 5) = 1234512345 StringUtil.wrap("12345", 10) = 12345

Parameters:
string - The String
lineSeparator - Line break, "
" by default
wrapLength - The position to create a line break
Returns:
String

getBytesUTF8

public static byte[] getBytesUTF8(String string)

getStringUTF8

public static String getStringUTF8(byte[] bytes)

escapeRegEx

public static String escapeRegEx(String regex)
Escapes metacharacters in a regex into literals.


search

public static StringUtil.SearchResult search(String toSearch,
                                             int fromIdx,
                                             Collection toFind)
Searches for the first one of any of the supplied strings

Parameters:
toSearch - the string to search
fromIdx - the index at which to begin the search
Returns:
the result of the search

parseMatchingParentheses

public static String[] parseMatchingParentheses(String str)


AribaWeb User Interface Development Framework
Copyright © 2000-2009 Ariba, Inc. All Rights Reserved.