|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.pf.text.StringUtil
public class StringUtil
The sole instance of this class provides several convienience methods for string manipulation such as substring replacement or character repetition.
Field Summary | |
---|---|
static char |
CH_CR
Constant for the carriage return character |
static char |
CH_NEWLINE
Constant for the new line character |
static char |
CH_SPACE
Constant for the space character |
static char |
CH_TAB
Constant for the tabulator character |
static String |
STR_CR
Constant for the String representation of the carriage return character |
static String |
STR_NEWLINE
Constant for the String representation of the new line character |
static String |
STR_SPACE
Constant for the String representation of the space character |
static String |
STR_TAB
Constant for the String representation of the tabulator character |
Constructor Summary | |
---|---|
StringUtil()
|
Method Summary | |
---|---|
String[] |
allParts(String text,
String delimiters)
Returns an array of substrings of the given text. |
String[] |
allSubstrings(String text,
String separator)
Returns the given text split up into an array of strings, at the occurrances of the separator string. |
String[] |
append(String[] strings,
String string)
Returns the given string array extended by one element that hold the specified string. |
String[] |
append(String[] strings,
String[] appendStrings)
Returns an array of strings that contains all strings given by the first and second string array. |
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. |
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. |
Map |
asMap(String str)
Returns a new map object that contains all key-value pairs of the specified string. |
Map |
asMap(String str,
String elementSeparator)
Returns a new map object that contains all key-value pairs of the specified string. |
Map |
asMap(String str,
String elementSeparator,
String keyValueSeparator)
Returns a new map object that contains all key-value pairs of the specified string. |
Properties |
asProperties(String str)
Adds all key-value pairs of the given string to a new properties object. |
String |
asString(String[] strings)
Returns a string that contains all given strings concatenated and separated by comma. |
String |
asString(String[] strings,
String separator)
Returns a string that contains all given strings concatenated and separated by the specified separator. |
String |
center(String str,
int len)
Returns the given string filled (on the right and right) up to the specified length with spaces. |
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. |
protected void |
collectParts(List list,
StringTokenizer tokenizer)
|
protected void |
collectParts(List list,
StringTokenizer tokenizer,
String delimiter)
|
boolean |
contains(String[] strArray,
String searchStr)
Returns whether or not the specified string can be found in the given string array. |
boolean |
contains(String[] strArray,
String searchStr,
boolean ignoreCase)
Returns whether or not the specified string can be found in the given string array. |
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. |
boolean |
containsIgnoreCase(String[] strArray,
String searchStr)
Returns whether or not the specified string can be found in the given string array. |
String[] |
copyFrom(String[] from,
int start)
Returns all elements of string array from in a new array from index start up to the end. |
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). |
static StringUtil |
current()
Returns the one and only instance of this class. |
String |
cutHead(String text,
String separator)
Returns the portion of the given string that stands after the last occurance of the specified separator. |
String |
cutTail(String text,
String separator)
Returns the portion of the given string that comes before the last occurance of the specified separator. |
String |
getDelimitedSubstring(String text,
String delimiter)
Returns the first substring that is enclosed by the specified delimiter. |
String |
getDelimitedSubstring(String text,
String startDelimiter,
String endDelimiter)
Returns the first substring that is enclosed by the specified delimiters. |
int |
indexOf(String[] strArray,
String searchStr)
Returns the index of the specified string in the given string array. |
int |
indexOf(String[] strArray,
StringPattern pattern)
Returns the index of the first string in the given string array that matches the specified string pattern. |
int |
indexOfIgnoreCase(String[] strArray,
String searchStr)
Returns the index of the specified string in the given string array. |
protected int |
indexOfString(String[] strArray,
String searchStr,
boolean ignoreCase)
|
String |
leftPad(int value,
int len)
Returns the given integer as string filled (on the left) up to the specified length with zeroes. |
String |
leftPad(String str,
int len)
Returns the given string filled (on the left) up to the specified length with spaces. |
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. |
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. |
protected String |
padCh(String str,
int len,
char ch,
boolean left)
|
String[] |
parts(String text,
String delimiters)
Returns an array of substrings of the given text. |
protected String[] |
parts(String text,
String delimiters,
boolean all)
Returns an array of substrings of the given text. |
String |
prefix(String str,
String separator)
Returns the substring of the given string that comes before the first occurance of the specified separator. |
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. |
String[] |
remove(String[] strings,
String removeString)
Removes the given string from the specified string array. |
String[] |
remove(String[] strings,
String[] removeStrings)
Removes all string of the second array from the first array. |
protected String[] |
removeFromStringArray(String[] strings,
String[] removeStrings)
Removes the given strings from the array. |
String[] |
removeNull(String[] strings)
Removes all null values from the given string array. |
String |
repeat(char ch,
int count)
Returns a string with size of count and all characters initialized with ch. |
String |
replaceAll(String sourceStr,
String oldSubStr,
String newSubStr)
Returns the given string with all found oldSubStr replaced by newSubStr. |
String |
reverse(String str)
Returns a string that contains all characters of the given string in reverse order. |
String |
rightPad(int value,
int len)
Returns the given integer as string filled (on the right) up to the specified length with spaces. |
String |
rightPad(String str,
int len)
Returns the given string filled (on the right) up to the specified length with spaces. |
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. |
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. |
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. |
String |
stackTrace(Throwable throwable)
Prints the stack trace of the specified throwable to a string and returns it. |
String |
startingFrom(String str,
String separator)
Returns the substring of the given string that comes after the first occurance of the specified separator. |
String[] |
substrings(String text,
String separator)
Returns the given text split up into an array of strings, at the occurrances of the separator string. |
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. |
String |
suffix(String str,
String separator)
Returns the substring of the given string that comes after the first occurance of the specified separator. |
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. |
Map |
toMap(String str,
Map map)
Adds all key-value pairs of the given string to the specified map. |
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. |
Map |
toMap(String str,
String elementSeparator,
String keyValueSeparator,
Map map)
Returns the given map with new entries from the specified String. |
Properties |
toProperties(String str,
Properties properties)
Adds all key-value pairs of the given string to the specified properties. |
protected String |
trimSeparator(String text,
String separator)
Cuts off all leading and trailing occurences of separator in text. |
String |
upTo(String str,
String separator)
Returns the substring of the given string that comes before the first occurance of the specified separator. |
String[] |
words(String text)
Returns an array of substrings of the given text. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final char CH_SPACE
public static final char CH_NEWLINE
public static final char CH_CR
public static final char CH_TAB
public static final String STR_SPACE
public static final String STR_NEWLINE
public static final String STR_CR
public static final String STR_TAB
Constructor Detail |
---|
public StringUtil()
Method Detail |
---|
public static StringUtil current()
public String replaceAll(String sourceStr, String oldSubStr, String newSubStr)
sourceStr
- The string that should be checked for occurrences of oldSubStroldSubStr
- The string that is searched for in sourceStrnewSubStr
- The new string that is placed everywhere the oldSubStr was found
public String repeat(char ch, int count)
ch
- the character to be repeated in the result string.count
- the number of times the given character should occur in the result string.
public String[] words(String text)
text
- The string that should be splitted into whitespace separated words
parts(String, String)
public String[] parts(String text, String delimiters)
text
- The string that should be splitted into substringsdelimiters
- All characters that should be recognized as a separator or substrings
allParts(String, String)
,
substrings(String, String)
,
allSubstrings(String, String)
public String[] allParts(String text, String delimiters)
Examples:
allParts( "/A/B//", "/" ) --> { "A", "B", "" }
allParts( "/A,B/C;D", ",;/" ) --> { "A", "B", "C", "D" }
allParts( "A/B,C/D", "," ) --> { "A/B", "C/D" }
text
- The string that should be splitted into substringsdelimiters
- All characters that should be recognized as a separator or substrings
parts(String, String)
,
substrings(String, String)
,
allSubstrings(String, String)
public String[] substrings(String text, String separator)
text
- The text to be split upseparator
- The string that separates the substrings
allSubstrings(String, String)
,
parts(String, String)
,
allParts(String, String)
public String[] allSubstrings(String text, String separator)
text
- The text to be split upseparator
- The string that separates the substrings
substrings(String, String)
,
parts(String, String)
,
allParts(String, String)
public String getDelimitedSubstring(String text, String startDelimiter, String endDelimiter)
Example:
getDelimitedSubstring( "This {placeholder} belongs to me", "{", "}" )
--> returns "placeholder"
text
- The input string that contains the delimited partstartDelimiter
- The start delimiter of the substringendDelimiter
- The end delimiter of the substring
public String getDelimitedSubstring(String text, String delimiter)
Example:
getDelimitedSubstring( "File 'text.txt' not found.", "'", "'" )
--> returns "text.txt"
text
- The input string that contains the delimited partdelimiter
- The start and end delimiter of the substring
public String stackTrace(Throwable throwable)
public String leftPadCh(String str, int len, char ch)
public String leftPad(String str, int len)
str
- The string that has to be filled up to the specified lengthlen
- The length of the result stringpublic String leftPadCh(int value, int len, char fillChar)
public String leftPad(int value, int len)
public String rightPadCh(String str, int len, char ch)
public String rightPad(String str, int len)
public String rightPadCh(int value, int len, char fillChar)
public String rightPad(int value, int len)
public String centerCh(String str, int len, char ch)
public String center(String str, int len)
public String[] append(String[] strings, String string)
public String[] append(String[] strings, String[] appendStrings)
strings
- The array of string to which to appendappendStrings
- The string to be appended to the first arraypublic String[] appendIfNotThere(String[] strings, String appendString)
public String[] appendIfNotThere(String[] strings, String[] appendStrings)
public String[] remove(String[] strings, String[] removeStrings)
strings
- The array from which to remove the stringsremoveStrings
- The strings to be removedpublic String[] remove(String[] strings, String removeString)
strings
- The array from which to remove the stringremoveString
- The string to be removedpublic String[] removeNull(String[] strings)
strings
- The array to be cleared of null valuespublic String asString(String[] strings, String separator)
strings
- The array of strings that should be concatenatedseparator
- The separator between the strings
public String asString(String[] strings)
strings
- The array of strings that should be concatenated
public int indexOf(String[] strArray, StringPattern pattern)
strArray
- An array of string (may contain null elements)pattern
- The pattern the searched string must match
public int indexOf(String[] strArray, String searchStr)
strArray
- An array of string (may contain null elements)searchStr
- The string to be looked up in the array (null allowed)
public int indexOfIgnoreCase(String[] strArray, String searchStr)
strArray
- An array of string (may contain null elements)searchStr
- The string to be looked up in the array (null allowed)
public boolean contains(String[] strArray, String searchStr, boolean ignoreCase)
strArray
- An array of string (may contain null elements)searchStr
- The string to be looked up in the array (null allowed)ignoreCase
- Defines whether or not the comparison is case-sensitive.
public boolean contains(String[] strArray, StringPattern pattern)
strArray
- An array of string (may contain null elements)pattern
- The string pattern to match against in the array (null allowed)
public boolean contains(String[] strArray, String searchStr)
strArray
- An array of string (may contain null elements)searchStr
- The string to be looked up in the array (null allowed)
public boolean containsIgnoreCase(String[] strArray, String searchStr)
strArray
- An array of string (may contain null elements)searchStr
- The string to be looked up in the array (null allowed)
public String[] copyFrom(String[] from, int start)
from
- The string array the elements should be copied fromstart
- Index of the first element to copypublic String[] copyFrom(String[] from, int start, int end)
from
- The string array the elements should be copied fromstart
- Index of the first element to copyend
- Index of last element to be copiedpublic String cutTail(String text, String separator)
Examples:
cutTail( "A/B/C", "/" ) ; // returns "A/B"
cutTail( "A/B/C", "," ) ; // returns "A/B/C"
text
- The string from which to cut off the tailseparator
- The separator from where to cut off
prefix( String, String )
,
suffix( String, String )
,
cutHead( String, String )
,
startingFrom( String, String )
,
upTo( String, String )
public String cutHead(String text, String separator)
Examples:
cutHead( "A/B/C", "/" ) ; // returns "C"
cutHead( "A/B/C", "," ) ; // returns "A/B/C"
text
- The string from which to cut off the headseparator
- The separator up to which to cut off
prefix( String, String )
,
cutTail( String, String )
,
suffix( String, String )
,
startingFrom( String, String )
,
upTo( String, String )
public String[] splitNameValue(String str, String separator)
str
- The string that contains the name-value pairseparator
- The separator between name and valuepublic String prefix(String str, String separator)
Examples:
prefix( "A/B/C", "/" ) ; // returns "A"
prefix( "A/B/C", "," ) ; // returns null
str
- The string of which the prefix is desiredseparator
- Separates the prefix from the rest of the stringsuffix( String, String )
,
cutTail( String, String )
,
cutHead( String, String )
,
startingFrom( String, String )
,
upTo( String, String )
public String suffix(String str, String separator)
Examples:
suffix( "A/B/C", "/" ) ; // returns "B/C"
suffix( "A/B/C", "," ) ; // returns null
str
- The string of which the suffix is desiredseparator
- Separates the suffix from the rest of the stringprefix( String, String )
,
cutTail( String, String )
,
cutHead( String, String )
,
startingFrom( String, String )
,
upTo( String, String )
public String upTo(String str, String separator)
Examples:
upTo( "A/B/C", "/" ) ; // returns "A"
upTo( "A/B/C", "," ) ; // returns "A/B/C"
upTo( "/A/B/C", "/" ) ; // returns ""
str
- The string of which the prefix is desiredseparator
- Separates the prefix from the rest of the stringprefix( String, String )
,
cutTail( String, String )
,
cutHead( String, String )
,
startingFrom( String, String )
,
suffix( String, String )
public String startingFrom(String str, String separator)
Examples:
startingFrom( "A/B/C", "/" ) ; // returns "B/C"
startingFrom( "A/B/C", "," ) ; // returns "A/B/C"
str
- The string of which the suffix is desiredseparator
- Separates the suffix from the rest of the stringprefix( String, String )
,
cutTail( String, String )
,
cutHead( String, String )
,
suffix( String, String )
,
upTo( String, String )
public String reverse(String str)
public Map toMap(String str, String elementSeparator, String keyValueSeparator, Map map)
Be aware that all leading and trailing whitespaces of keys and values will be removed!
str
- The string that contains the list of key-value pairselementSeparator
- The separator between the elements of the listkeyValueSeparator
- The separator between the keys and valuesmap
- The map to which the key-value pairs are addedpublic Map asMap(String str)
Example:
"main=Fred,support1=John,support2=Stella,manager=Oscar"
Be aware that all leading and trailing whitespaces of keys and values will be removed!
str
- The string with the list of key-value pairspublic Map asMap(String str, String elementSeparator)
Be aware that all leading and trailing whitespaces of keys and values will be removed!
str
- The string that contains the list of key-value pairselementSeparator
- The separator between the elements of the listpublic Map asMap(String str, String elementSeparator, String keyValueSeparator)
Be aware that all leading and trailing whitespaces of keys and values will be removed!
str
- The string that contains the list of key-value pairselementSeparator
- The separator between the elements of the listkeyValueSeparator
- The separator between the keys and valuespublic Map toMap(String str, String elementSeparator, Map map)
Be aware that all leading and trailing whitespaces of keys and values will be removed!
str
- The string that contains the list of key-value pairselementSeparator
- The separator between the elements of the listmap
- The map to which the key-value pairs are addedpublic Map toMap(String str, Map map)
Be aware that all leading and trailing whitespaces of keys and values will be removed!
str
- The string that contains the list of key-value pairsmap
- The map to which the key-value pairs are addedpublic Properties asProperties(String str)
Be aware that all leading and trailing whitespaces of keys and values will be removed!
str
- The string that contains the list of key-value pairspublic Properties toProperties(String str, Properties properties)
Be aware that all leading and trailing whitespaces of keys and values will be removed!
str
- The string that contains the list of key-value pairsproperties
- The properties where the key-value pairs should be addedprotected String trimSeparator(String text, String separator)
protected String[] parts(String text, String delimiters, boolean all)
text
- The string that should be splitted into substringsdelimiters
- All characters that should be recognized as a separator or substringsall
- If true, empty elements will be returned, otherwise thye are skipped
protected void collectParts(List list, StringTokenizer tokenizer)
protected void collectParts(List list, StringTokenizer tokenizer, String delimiter)
protected String[] substrings(String text, String separator, boolean all)
text
- The text to be split upseparator
- The string that separates the substringsall
- If true, empty strings are added, otherwise skipped
parts(String, String, boolean)
protected String padCh(String str, int len, char ch, boolean left)
protected int indexOfString(String[] strArray, String searchStr, boolean ignoreCase)
protected String prefix(String str, String separator, boolean returnNull)
str
- The string of which the prefix is desiredseparator
- Separates the prefix from the rest of the stringreturnNull
- Specifies if null will be returned if no separator is foundprotected String suffix(String str, String separator, boolean returnNull)
str
- The string of which the suffix is desiredseparator
- Separates the suffix from the rest of the stringreturnNull
- Specifies if null will be returned if no separator is foundprotected String[] removeFromStringArray(String[] strings, String[] removeStrings)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |