Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.daemon.util » [javadoc | source]
com.sshtools.daemon.util
public class: StringUtil [javadoc | source]
java.lang.Object
   com.sshtools.daemon.util.StringUtil
The sole instance of this class provides several convienience methods for string manipulation such as substring replacement or character repetition.
Field Summary
public static final  char CH_SPACE    Constant for the space character 
public static final  char CH_NEWLINE    Constant for the new line character 
public static final  char CH_CR    Constant for the carriage return character 
public static final  char CH_TAB    Constant for the tabulator character 
public static final  String STR_SPACE    Constant for the String representation of the space character 
public static final  String STR_NEWLINE    Constant for the String representation of the new line character 
public static final  String STR_CR    Constant for the String representation of the carriage return character 
public static final  String STR_TAB    Constant for the String representation of the tabulator character 
Method from com.sshtools.daemon.util.StringUtil Summary:
allParts,   allSubstrings,   append,   append,   appendIfNotThere,   appendIfNotThere,   asMap,   asMap,   asMap,   asProperties,   asString,   asString,   center,   centerCh,   collectParts,   collectParts,   contains,   contains,   contains,   containsIgnoreCase,   copyFrom,   copyFrom,   current,   cutHead,   cutTail,   getDelimitedSubstring,   getDelimitedSubstring,   indexOf,   indexOf,   indexOfIgnoreCase,   indexOfString,   leftPad,   leftPad,   leftPadCh,   leftPadCh,   padCh,   parts,   parts,   prefix,   prefix,   remove,   remove,   removeFromStringArray,   removeNull,   repeat,   replaceAll,   reverse,   rightPad,   rightPad,   rightPadCh,   rightPadCh,   splitNameValue,   stackTrace,   startingFrom,   substrings,   substrings,   suffix,   suffix,   toMap,   toMap,   toMap,   toProperties,   trimSeparator,   upTo,   words
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from com.sshtools.daemon.util.StringUtil Detail:
 public String[] allParts(String text,
    String delimiters) 
    Returns an array of substrings of the given text.
    The separators between the substrings are the given delimiters. Each character in the delimiter string is treated as a separator.
    For each delimiter that is followed immediately by another delimiter an empty string will be added to the result. There are no empty strings added to the result for a delimiter at the very beginning of at the very end.

    Examples:

    allParts( "/A/B//", "/" ) --> { "A", "B", "" }
    allParts( "/A,B/C;D", ",;/" ) --> { "A", "B", "C", "D" }
    allParts( "A/B,C/D", "," ) --> { "A/B", "C/D" }

 public String[] allSubstrings(String text,
    String separator) 
    Returns the given text split up into an array of strings, at the occurrances of the separator string. In contrary to method allParts() the separator is a one or many character sequence delimiter. That is, only the exact sequence of the characters in separator identifies the end of a substring. Subsequent occurences of separator are not skipped. They are added as empty strings to the result.
 public String[] append(String[] strings,
    String string) 
    Returns the given string array extended by one element that hold the specified string.
 public String[] append(String[] strings,
    String[] appendStrings) 
    Returns an array of strings that contains all strings given by the first and second string array. The strings from the second array will be added at the end of the first array.
 public String[] appendIfNotThere(String[] strings,
    String appendString) 
    Returns an array of strings that contains all strings given in the first plus the specified string to append, if it is not already in the given array.
 public String[] appendIfNotThere(String[] strings,
    String[] appendStrings) 
    Returns an array of strings that contains all strings given in the first plus all strings of the second array that are not already in the first array.
 public Map asMap(String str) 
    Returns a new map object that contains all key-value pairs of the specified string.
    The separator between the elements is assumed to be "," and "=" between key and value.

    Example:
    "main=Fred,support1=John,support2=Stella,manager=Oscar"

    Be aware that all leading and trailing whitespaces of keys and values will be removed!

 public Map asMap(String str,
    String elementSeparator) 
    Returns a new map object that contains all key-value pairs of the specified string.
    The separator between the keys and values is assumed to be "=".

    Be aware that all leading and trailing whitespaces of keys and values will be removed!

 public Map asMap(String str,
    String elementSeparator,
    String keyValueSeparator) 
    Returns a new map object that contains all key-value pairs of the specified string.

    Be aware that all leading and trailing whitespaces of keys and values will be removed!

 public Properties asProperties(String str) 
    Adds all key-value pairs of the given string to a new properties object.
    The separator between the elements is assumed to be "," and "=" between key and value.

    Be aware that all leading and trailing whitespaces of keys and values will be removed!

 public String asString(String[] strings) 
    Returns a string that contains all given strings concatenated and separated by comma.
 public String asString(String[] strings,
    String separator) 
    Returns a string that contains all given strings concatenated and separated by the specified separator.
 public String center(String str,
    int len) 
    Returns the given string filled (on the right and right) up to the specified length with spaces.
    Example: center( "Mike", 10 ) --> " Mike "
 public String centerCh(String str,
    int len,
    char ch) 
    Returns the given string filled equally left and right up to the specified length with the given character.
    Example: centerCh( "A", 5, '_' ) --> "__A__"
    Example: centerCh( "XX", 7, '+' ) --> "++XX+++"
 protected  void collectParts(List list,
    StringTokenizer tokenizer) 
 protected  void collectParts(List list,
    StringTokenizer tokenizer,
    String delimiter) 
 public boolean contains(String[] strArray,
    StringPattern pattern) 
    Returns whether or not a string can be found in the given string array that matches the specified string pattern.
 public boolean contains(String[] strArray,
    String searchStr) 
    Returns whether or not the specified string can be found in the given string array. The comparison of the strings is case-sensitive!
 public boolean contains(String[] strArray,
    String searchStr,
    boolean ignoreCase) 
    Returns whether or not the specified string can be found in the given string array.
 public boolean containsIgnoreCase(String[] strArray,
    String searchStr) 
    Returns whether or not the specified string can be found in the given string array. The comparison of the strings is case-insensitive!
 public String[] copyFrom(String[] from,
    int start) 
    Returns all elements of string array from in a new array from index start up to the end. If start index is larger than the array's length, an empty array will be returned.
 public String[] copyFrom(String[] from,
    int start,
    int end) 
    Returns all elements of string array from in a new array from index start up to index end (inclusive). If end is larger than the last valid index, it will be reduced to the last index. If end index is less than start index, an empty array will be returned.
 public static StringUtil current() 
    Returns the one and only instance of this class.
 public String cutHead(String text,
    String separator) 
    Returns the portion of the given string that stands after the last occurance of the specified separator.
    If the separator could not be found in the given string, then the string is returned unchanged.

    Examples:

    cutHead( "A/B/C", "/" ) ; // returns "C"
    cutHead( "A/B/C", "," ) ; // returns "A/B/C"

 public String cutTail(String text,
    String separator) 
    Returns the portion of the given string that comes before the last occurance of the specified separator.
    If the separator could not be found in the given string, then the string is returned unchanged.

    Examples:

    cutTail( "A/B/C", "/" ) ; // returns "A/B"
    cutTail( "A/B/C", "," ) ; // returns "A/B/C"

 public String getDelimitedSubstring(String text,
    String delimiter) 
    Returns the first substring that is enclosed by the specified delimiter.
    The delimiters are not included in the return string.

    Example:
    getDelimitedSubstring( "File 'text.txt' not found.", "'", "'" ) --> returns "text.txt"

 public String getDelimitedSubstring(String text,
    String startDelimiter,
    String endDelimiter) 
    Returns the first substring that is enclosed by the specified delimiters.
    The delimiters are not included in the return string.

    Example:
    getDelimitedSubstring( "This {placeholder} belongs to me", "{", "}" ) --> returns "placeholder"

 public int indexOf(String[] strArray,
    StringPattern pattern) 
    Returns the index of the first string in the given string array that matches the specified string pattern. If no string is found in the array the result is -1.
 public int indexOf(String[] strArray,
    String searchStr) 
    Returns the index of the specified string in the given string array. It returns the index of the first occurrence of the string. If the string is not found in the array the result is -1. The comparison of the strings is case-sensitive!
 public int indexOfIgnoreCase(String[] strArray,
    String searchStr) 
    Returns the index of the specified string in the given string array. It returns the index of the first occurrence of the string. If the string is not found in the array the result is -1. The comparison of the strings is case-insensitive!
 protected int indexOfString(String[] strArray,
    String searchStr,
    boolean ignoreCase) 
 public String leftPad(String str,
    int len) 
    Returns the given string filled (on the left) up to the specified length with spaces.
    Example: leftPad( "XX", 4 ) --> " XX"
 public String leftPad(int value,
    int len) 
    Returns the given integer as string filled (on the left) up to the specified length with zeroes.
    Example: leftPad( 12, 4 ) --> "0012"
 public String leftPadCh(String str,
    int len,
    char ch) 
    Returns the given string filled (on the left) up to the specified length with the given character.
    Example: leftPadCh( "12", 6, '0' ) --> "000012"
 public String leftPadCh(int value,
    int len,
    char fillChar) 
    Returns the given integer as string filled (on the left) up to the specified length with the given fill character.
    Example: leftPad( 24, 5, '' ) --> "24"
 protected String padCh(String str,
    int len,
    char ch,
    boolean left) 
 public String[] parts(String text,
    String delimiters) 
    Returns an array of substrings of the given text.
    The separators between the substrings are the given delimiters. Each character in the delimiter string is treated as a separator.
    All consecutive delimiters are treated as one delimiter, that is there will be no empty strings in the result.
 protected String[] parts(String text,
    String delimiters,
    boolean all) 
    Returns an array of substrings of the given text.
    The separators between the substrings are the given delimiters. Each character in the delimiter string is treated as a separator.
 public String prefix(String str,
    String separator) 
    Returns the substring of the given string that comes before the first occurance of the specified separator. If the string starts with a separator, the result will be an empty string. If the string doesn't contain the separator the method returns null.

    Examples:

    prefix( "A/B/C", "/" ) ; // returns "A"
    prefix( "A/B/C", "," ) ; // returns null

 protected String prefix(String str,
    String separator,
    boolean returnNull) 
    Returns the substring of the given string that comes before the first occurance of the specified separator. If the string starts with a separator, the result will be an empty string. If the string doesn't contain the separator the method returns null or the whole string, depending on the returnNull flag.
 public String[] remove(String[] strings,
    String[] removeStrings) 
    Removes all string of the second array from the first array. Returns a new array of string that contains all remaining strings of the original strings array.
 public String[] remove(String[] strings,
    String removeString) 
    Removes the given string from the specified string array. Returns a new array of string that contains all remaining strings of the original strings array.
 protected String[] removeFromStringArray(String[] strings,
    String[] removeStrings) 
    Removes the given strings from the array. If removeStrings is null it means that all null values are removed from the first array.
 public String[] removeNull(String[] strings) 
    Removes all null values from the given string array. Returns a new string array that contains all none null values of the input array.
 public String repeat(char ch,
    int count) 
    Returns a string with size of count and all characters initialized with ch.
 public String replaceAll(String sourceStr,
    String oldSubStr,
    String newSubStr) 
    Returns the given string with all found oldSubStr replaced by newSubStr.
    Example: StringUtil.current().replaceAll( "Seven of ten", "even", "ix" ) ;
    results in: "Six of ten"
 public String reverse(String str) 
    Returns a string that contains all characters of the given string in reverse order.
 public String rightPad(String str,
    int len) 
    Returns the given string filled (on the right) up to the specified length with spaces.
    Example: rightPad( "88", 6 ) --> "88 "
 public String rightPad(int value,
    int len) 
    Returns the given integer as string filled (on the right) up to the specified length with spaces.
    Example: rightPad( "17", 5 ) --> "17 "
 public String rightPadCh(String str,
    int len,
    char ch) 
    Returns the given string filled (on the right) up to the specified length with the given character.
    Example: rightPadCh( "34", 5, 'X' ) --> "34XXX"
 public String rightPadCh(int value,
    int len,
    char fillChar) 
    Returns the given integer as string filled (on the right) up to the specified length with the given character.
    Example: rightPad( "32", 4, '#' ) --> "32##"
 public String[] splitNameValue(String str,
    String separator) 
    Returns a string array with two elements where the first is the attribute name and the second is the attribute value. Splits the given string at the first occurance of separator and returns the piece before the separator in element 0 and the piece after the separator in the returned array. If the separator is not found, the first element contains the full string and the second an empty string.
 public String stackTrace(Throwable throwable) 
    Prints the stack trace of the specified throwable to a string and returns it.
 public String startingFrom(String str,
    String separator) 
    Returns the substring of the given string that comes after the first occurance of the specified separator. If the string doesn't contain the separator the method returns the whole string unchanged.

    Examples:

    startingFrom( "A/B/C", "/" ) ; // returns "B/C"
    startingFrom( "A/B/C", "," ) ; // returns "A/B/C"

 public String[] substrings(String text,
    String separator) 
    Returns the given text split up into an array of strings, at the occurrances of the separator string. In contrary to method parts() the separator is a one or many character sequence delimiter. That is, only the exact sequence of the characters in separator identifies the end of a substring. Subsequent occurences of separator will be skipped. Therefore no empty strings ("") will be in the result array.
 protected String[] substrings(String text,
    String separator,
    boolean all) 
    Returns the given text split up into an array of strings, at the occurrances of the separator string. In contrary to method parts() the separator is a one or many character sequence delimiter. That is, only the exact sequence of the characters in separator identifies the end of a substring. Parameter all defines whether empty strings between consecutive separators are added to the result or not.
 public String suffix(String str,
    String separator) 
    Returns the substring of the given string that comes after the first occurance of the specified separator. If the string ends with a separator, the result will be an empty string. If the string doesn't contain the separator the method returns null.

    Examples:

    suffix( "A/B/C", "/" ) ; // returns "B/C"
    suffix( "A/B/C", "," ) ; // returns null

 protected String suffix(String str,
    String separator,
    boolean returnNull) 
    Returns the substring of the given string that comes after the first occurance of the specified separator. If the string ends with a separator, the result will be an empty string. If the string doesn't contain the separator the method returns null or the whole string, depending on the returnNull flag.
 public Map toMap(String str,
    Map map) 
    Adds all key-value pairs of the given string to the specified map.
    The separator between the elements is assumed to be "," and "=" between key and value.

    Be aware that all leading and trailing whitespaces of keys and values will be removed!

 public Map toMap(String str,
    String elementSeparator,
    Map map) 
    Returns the given map object with all key-value pairs of the specified string added to it.
    The separator between the keys and values is assumed to be "=".

    Be aware that all leading and trailing whitespaces of keys and values will be removed!

 public Map toMap(String str,
    String elementSeparator,
    String keyValueSeparator,
    Map map) 
    Returns the given map with new entries from the specified String. If the specified map is null a new empty java.util.Hashtable will be created.
    The string is split up into elements separated by the elementSeparator parameter. If this parameter is null the default separator "," is used.
    After that each part is split up to a key-value pair separated by the keyValueSeparator parameter. If this parameter is null the default "=" is used.
    Then the key-value pairs are added to the map and the map is returned.

    Be aware that all leading and trailing whitespaces of keys and values will be removed!

 public Properties toProperties(String str,
    Properties properties) 
    Adds all key-value pairs of the given string to the specified properties.
    The separator between the elements is assumed to be "," and "=" between key and value.

    Be aware that all leading and trailing whitespaces of keys and values will be removed!

 protected String trimSeparator(String text,
    String separator) 
    Cuts off all leading and trailing occurences of separator in text.
 public String upTo(String str,
    String separator) 
    Returns the substring of the given string that comes before the first occurance of the specified separator. If the string starts with a separator, the result will be an empty string. If the string doesn't contain the separator the method returns the whole string unchanged.

    Examples:

    upTo( "A/B/C", "/" ) ; // returns "A"
    upTo( "A/B/C", "," ) ; // returns "A/B/C"
    upTo( "/A/B/C", "/" ) ; // returns ""

 public String[] words(String text) 
    Returns an array of substrings of the given text.
    The delimiters between the substrings are the whitespace characters SPACE, NEWLINE, CR and TAB.