| Method from com.sshtools.daemon.util.StringUtil Detail: |
public String[] allParts(String text,
String delimiters) {
return this.parts(text, delimiters, true);
}
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) {
return this.substrings(text, separator, true);
}
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) {
String[] appStr = { string };
return this.append(strings, appStr);
}
Returns the given string array extended by one element that hold the
specified string. |
public String[] append(String[] strings,
String[] appendStrings) {
String[] newStrings = null;
if (strings == null) {
return appendStrings;
}
if (appendStrings == null) {
return strings;
}
newStrings = new String[strings.length + appendStrings.length];
System.arraycopy(strings, 0, newStrings, 0, strings.length);
System.arraycopy(appendStrings, 0, newStrings, strings.length,
appendStrings.length);
return newStrings;
}
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) {
if (this.contains(strings, appendString)) {
return strings;
} else {
return this.append(strings, 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) {
String[] newStrings = strings;
if (appendStrings == null) {
return newStrings;
}
for (int i = 0; i < appendStrings.length; i++) {
newStrings = this.appendIfNotThere(newStrings, appendStrings[i]);
}
return newStrings;
}
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) {
return this.toMap(str, null, null, null);
}
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) {
return this.toMap(str, elementSeparator, null, null);
}
|
public Map asMap(String str,
String elementSeparator,
String keyValueSeparator) {
return this.toMap(str, elementSeparator, keyValueSeparator, null);
}
|
public Properties asProperties(String str) {
return this.toProperties(str, null);
}
|
public String asString(String[] strings) {
return this.asString(strings, ",");
}
Returns a string that contains all given strings concatenated and
separated by comma. |
public String asString(String[] strings,
String separator) {
StringBuffer buffer = null;
buffer = new StringBuffer(strings.length * 20);
if (strings.length > 0) {
buffer.append(strings[0].toString());
for (int i = 1; i < strings.length; i++) {
buffer.append(separator);
if (strings[i] != null) {
buffer.append(strings[i]);
}
}
}
return buffer.toString();
}
Returns a string that contains all given strings concatenated and
separated by the specified separator. |
public String center(String str,
int len) {
return this.centerCh(str, len, CH_SPACE);
}
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) {
String buffer = null;
int missing = len - str.length();
int half = 0;
if (missing < = 0) {
return str;
}
half = missing / 2;
buffer = this.rightPadCh(str, len - half, ch);
return this.leftPadCh(buffer, len, 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) {
while (tokenizer.hasMoreTokens()) {
list.add(tokenizer.nextToken());
}
}
|
protected void collectParts(List list,
StringTokenizer tokenizer,
String delimiter) {
String token;
boolean lastWasDelimiter = false;
while (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
if (delimiter.indexOf(token) >= 0) {
if (lastWasDelimiter) {
list.add("");
}
lastWasDelimiter = true;
} else {
list.add(token);
lastWasDelimiter = false;
}
}
}
|
public boolean contains(String[] strArray,
StringPattern pattern) {
return (this.indexOf(strArray, pattern) >= 0);
}
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) {
return (this.indexOf(strArray, searchStr) >= 0);
}
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) {
if (ignoreCase) {
return this.containsIgnoreCase(strArray, searchStr);
} else {
return this.contains(strArray, searchStr);
}
}
Returns whether or not the specified string can be found in the given
string array. |
public boolean containsIgnoreCase(String[] strArray,
String searchStr) {
return (this.indexOfIgnoreCase(strArray, searchStr) >= 0);
}
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) {
if (from == null) {
return null;
}
return this.copyFrom(from, start, from.length - 1);
}
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) {
String[] result;
int count;
int stop = end;
if (from == null) {
return null;
}
if (stop > (from.length - 1)) {
stop = from.length - 1;
}
count = stop - start + 1;
if (count < 1) {
return new String[0];
}
result = new String[count];
System.arraycopy(from, start, result, 0, count);
return result;
}
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() {
if (getSingleton() == null) {
setSingleton(new StringUtil());
}
return getSingleton();
}
Returns the one and only instance of this class. |
public String cutHead(String text,
String separator) {
int index;
if ((text == null) || (separator == null)) {
return text;
}
index = text.lastIndexOf(separator);
if (index < 0) {
return text;
}
return text.substring(index + 1);
}
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) {
int index;
if ((text == null) || (separator == null)) {
return text;
}
index = text.lastIndexOf(separator);
if (index < 0) {
return text;
}
return text.substring(0, index);
}
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) {
return this.getDelimitedSubstring(text, delimiter, 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) {
int start;
int stop;
String subStr = "";
if ((text != null) && (startDelimiter != null) &&
(endDelimiter != null)) {
start = text.indexOf(startDelimiter);
if (start >= 0) {
stop = text.indexOf(endDelimiter, start + 1);
if (stop > start) {
subStr = text.substring(start + 1, stop);
}
}
}
return subStr;
}
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) {
if ((strArray == null) || (strArray.length == 0)) {
return -1;
}
boolean found = false;
for (int i = 0; i < strArray.length; i++) {
if (strArray[i] == null) {
if (pattern == null) {
found = true;
}
} else {
if (pattern != null) {
found = pattern.matches(strArray[i]);
}
}
if (found) {
return i;
}
}
return -1;
}
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) {
return this.indexOfString(strArray, searchStr, false);
}
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) {
return this.indexOfString(strArray, searchStr, true);
}
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) {
if ((strArray == null) || (strArray.length == 0)) {
return -1;
}
boolean found = false;
for (int i = 0; i < strArray.length; i++) {
if (strArray[i] == null) {
if (searchStr == null) {
found = true;
}
} else {
if (ignoreCase) {
found = strArray[i].equalsIgnoreCase(searchStr);
} else {
found = strArray[i].equals(searchStr);
}
}
if (found) {
return i;
}
}
return -1;
}
|
public String leftPad(String str,
int len) {
return this.leftPadCh(str, len, CH_SPACE);
}
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) {
return this.leftPadCh(value, len, '0");
}
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) {
return this.padCh(str, len, ch, true);
}
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) {
return this.leftPadCh(Integer.toString(value), len, 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) {
StringBuffer buffer = null;
int missing = len - str.length();
if (missing < = 0) {
return str;
}
buffer = new StringBuffer(len);
if (!left) {
buffer.append(str);
}
for (int i = 1; i < = missing; i++) {
buffer.append(ch);
}
if (left) {
buffer.append(str);
}
return buffer.toString();
}
|
public String[] parts(String text,
String delimiters) {
return this.parts(text, delimiters, false);
}
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) {
ArrayList result = null;
StringTokenizer tokenizer = null;
if (text == null) {
return null;
}
if ((delimiters == null) || (delimiters.length() == 0)) {
String[] resultArray = { text };
return resultArray;
}
if (text.length() == 0) {
return new String[0];
} else {
result = new ArrayList();
tokenizer = new StringTokenizer(text, delimiters, all);
if (all) {
this.collectParts(result, tokenizer, delimiters);
} else {
this.collectParts(result, tokenizer);
}
}
return (String[]) result.toArray(new String[0]);
}
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) {
return this.prefix(str, separator, true);
}
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) {
if (str == null) {
return null;
}
if (separator == null) {
return (returnNull ? null : str);
}
int index = str.indexOf(separator);
if (index >= 0) {
return str.substring(0, index);
} else {
return (returnNull ? null : str);
}
}
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) {
if ((strings == null) || (removeStrings == null) ||
(strings.length == 0) || (removeStrings.length == 0)) {
return strings;
}
return this.removeFromStringArray(strings, 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) {
String[] removeStrings = { removeString };
return this.remove(strings, removeStrings);
}
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) {
List list;
boolean remains;
list = new ArrayList(strings.length);
for (int i = 0; i < strings.length; i++) {
if (removeStrings == null) {
remains = strings[i] != null;
} else {
remains = !this.contains(removeStrings, strings[i]);
}
if (remains) {
list.add(strings[i]);
}
}
return (String[]) list.toArray(new String[list.size()]);
}
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) {
if (strings == null) {
return strings;
}
return this.removeFromStringArray(strings, null);
}
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) {
StringBuffer buffer = null;
buffer = new StringBuffer(count);
for (int i = 1; i < = count; i++) {
buffer.append(ch);
}
return (buffer.toString());
}
Returns a string with size of count and all characters initialized with
ch.
|
public String replaceAll(String sourceStr,
String oldSubStr,
String newSubStr) {
String part = null;
String result = "";
int index = -1;
int subLen = 0;
subLen = oldSubStr.length();
part = sourceStr;
while ((part.length() > 0) && (subLen > 0)) {
index = part.indexOf(oldSubStr);
if (index >= 0) {
result = result + part.substring(0, index) + newSubStr;
part = part.substring(index + subLen);
} else {
result = result + part;
part = "";
}
}
// while
return result;
}
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) {
if (str == null) {
return null;
}
char[] newStr = new char[str.length()];
StringCharacterIterator iterator = new StringCharacterIterator(str);
int i = 0;
for (char ch = iterator.last(); ch != CharacterIterator.DONE;
ch = iterator.previous()) {
newStr[i] = ch;
i++;
}
return new String(newStr);
}
Returns a string that contains all characters of the given string in
reverse order. |
public String rightPad(String str,
int len) {
return this.rightPadCh(str, len, CH_SPACE);
}
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) {
return this.rightPadCh(value, len, CH_SPACE);
}
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) {
return this.padCh(str, len, ch, false);
}
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) {
return this.rightPadCh(Integer.toString(value), len, 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) {
String[] result = { "", "" };
int index;
if (str != null) {
index = str.indexOf(separator);
if (index > 0) {
result[0] = str.substring(0, index);
result[1] = str.substring(index + separator.length());
} else {
result[0] = str;
}
}
return result;
}
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) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
throwable.printStackTrace(pw);
pw.close();
return sw.toString();
}
Prints the stack trace of the specified throwable to a string and
returns it. |
public String startingFrom(String str,
String separator) {
return this.suffix(str, separator, false);
}
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) {
return this.substrings(text, separator, false);
}
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) {
int index = 0;
int start = 0;
int sepLen = 0;
int strLen = 0;
String str = text;
ArrayList strings = new ArrayList();
if (text == null) {
return new String[0];
}
if ((separator == null) || (separator.length() == 0)) {
if (text.length() == 0) {
return new String[0];
}
String[] resultArray = { text };
return resultArray;
}
if (!all) {
str = this.trimSeparator(text, separator);
}
strLen = str.length();
if (strLen > 0) {
sepLen = separator.length();
index = str.indexOf(separator, start);
while (index >= 0) {
if (all) {
if (index > 0) {
strings.add(str.substring(start, index));
}
} else {
if (index > (start + sepLen)) {
strings.add(str.substring(start, index));
}
}
start = index + sepLen;
index = str.indexOf(separator, start);
}
if (start < strLen) {
strings.add(str.substring(start));
}
}
return (String[]) strings.toArray(new String[0]);
}
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) {
return this.suffix(str, separator, true);
}
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) {
if (str == null) {
return null;
}
if (separator == null) {
return (returnNull ? null : str);
}
int index = str.indexOf(separator);
if (index >= 0) {
return str.substring(index + separator.length());
} else {
return (returnNull ? null : str);
}
}
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) {
return this.toMap(str, null, null, map);
}
|
public Map toMap(String str,
String elementSeparator,
Map map) {
return this.toMap(str, elementSeparator, null, map);
}
|
public Map toMap(String str,
String elementSeparator,
String keyValueSeparator,
Map map) {
Map result;
String elemSep;
String kvSep;
String[] assignments;
String[] nameValue;
if (str == null) {
return map;
}
result = ((map == null) ? new Hashtable() : map);
elemSep = (elementSeparator == null) ? "," : elementSeparator;
kvSep = (keyValueSeparator == null) ? "=" : keyValueSeparator;
assignments = this.parts(str, elemSep);
for (int i = 0; i < assignments.length; i++) {
nameValue = this.splitNameValue(assignments[i], kvSep);
nameValue[0] = nameValue[0].trim();
nameValue[1] = nameValue[1].trim();
if (nameValue[0].length() > 0) {
result.put(nameValue[0], nameValue[1]);
}
}
return result;
}
|
public Properties toProperties(String str,
Properties properties) {
Properties props = (properties == null) ? new Properties() : properties;
return (Properties) this.toMap(str, null, null, props);
}
|
protected String trimSeparator(String text,
String separator) {
int sepLen = separator.length();
while (text.startsWith(separator)) {
text = text.substring(separator.length());
}
while (text.endsWith(separator)) {
text = text.substring(0, text.length() - sepLen);
}
return text;
}
Cuts off all leading and trailing occurences of separator in text. |
public String upTo(String str,
String separator) {
return this.prefix(str, separator, false);
}
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) {
return this.parts(text, WORD_DELIM);
}
Returns an array of substrings of the given text.
The delimiters between the substrings are the whitespace characters
SPACE, NEWLINE, CR and TAB. |