Package org.intermine.metadata
Class StringUtil
- java.lang.Object
-
- org.intermine.metadata.StringUtil
-
public final class StringUtil extends java.lang.Object
Collection of commonly used String utilities- Author:
- Andrew Varley
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StringUtil.LineWrappedString
Class for returning multiple values from the wrapLines method.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
allDigits(java.lang.String s)
Return true if all characters in a given String are digits.static java.lang.String
bufferToHexString(byte[] buffer)
Convert a byte buffer to a hexadecimal string.static java.lang.String
capitalise(java.lang.String str)
Returns a capitalised version of the given Stringstatic java.lang.String
colonsToDots(java.lang.String in)
Converts all the colons in a String into dots.static int
countOccurances(java.lang.String str, java.lang.String target)
Returns the number of occurances of str in targetstatic java.lang.String
decapitalise(java.lang.String str)
Returns a decapitalised version of the given String unless string is an acronym.static java.lang.String
duplicateQuotes(java.lang.String s)
Duplicates single quotes in Strings for correct storage in postgres.static java.lang.String
escapeBackslash(java.lang.String s)
Escape single backslash with single forwardslash for correct storage in postgres.static java.lang.String
escapeWithBackslashes(java.lang.String str)
Escapes single quotes and backslashes, with backslashes.static java.lang.String
getFlattenedSourceName(java.lang.String sourceName)
Flattens source name.static java.lang.String
indefiniteArticle(java.lang.String s)
Return 'a' or 'an' according to first letter of the given article.static java.lang.String
join(java.util.Collection<?> c, java.lang.String delimiter)
Returns a String formed by the delimited results of calling toString over a collection.static java.lang.String
pluralise(java.lang.String str)
Returns a pluralised version of the given Stringstatic java.lang.String
prettyList(java.util.Collection<java.lang.String> elements)
Take a collection of Strings and return a combined string as a comma separated list with 'and' between the final pair.static java.lang.String
prettyList(java.util.Collection<java.lang.String> elements, boolean sort)
Take a collection of Strings and return a combined string as a comma separated list with 'and' between the final pair.static java.lang.String
reverseCapitalisation(java.lang.String str)
Reverses the capitalisation of the first character of the given String.static java.util.Map<java.lang.String,java.lang.String>
serializedSortOrderToMap(java.lang.String str)
Make a Map from the serialized String returned by jQuery.sortable("serialize").static void
setNextUniqueNumber(long number)
Sets the number that is used to generate the next uniqueString.static java.lang.String[]
split(java.lang.String str, java.lang.String delim)
Returns a list of tokens delimited by delim in String str.static java.util.List<java.lang.String>
tokenize(java.lang.String str)
Returns a list of tokens delimited by whitespace in String str (useful when handling XML)static java.util.Collection<java.lang.String>
tokenize(java.lang.String strings, boolean lowercase)
Returns a list of tokens delimited by comma.static java.util.List<java.lang.String>
tokenize(java.lang.String str, java.lang.String delimiter)
static java.lang.String
toSameInitialCase(java.lang.String n, java.lang.String template)
Returns a string with the same initial letter case as the template stringstatic java.lang.String
trimLeft(java.lang.String s)
trim left space in stringstatic java.lang.String
trimSlashes(java.lang.String s)
Trim starting and trailing '/' characters from a string if present.static java.lang.String
uniqueString()
Returns a String that is different every timestatic StringUtil.LineWrappedString
wrapLines(java.lang.String input, int lineLength, int lineCount)
Wraps the given String into several lines and ultimately truncates it with an ellipsis.static StringUtil.LineWrappedString
wrapLines(java.lang.String input, int lineLength, int lineCount, int lastLineShorter)
Wraps the given String into several lines and ultimately truncates it with an ellipsis.
-
-
-
Method Detail
-
getFlattenedSourceName
public static java.lang.String getFlattenedSourceName(java.lang.String sourceName)
Flattens source name. e.g. LongOligo changes to long-oligo- Parameters:
sourceName
- the String to alter- Returns:
- the altered string
-
countOccurances
public static int countOccurances(java.lang.String str, java.lang.String target)
Returns the number of occurances of str in target- Parameters:
str
- the String to counttarget
- the String to look in- Returns:
- the number of occurances of str in target
- Throws:
java.lang.NullPointerException
- if either str or target are null
-
join
public static java.lang.String join(java.util.Collection<?> c, java.lang.String delimiter)
Returns a String formed by the delimited results of calling toString over a collection.- Parameters:
c
- the collection to stringifydelimiter
- the character to join on- Returns:
- the string representation
-
tokenize
public static java.util.List<java.lang.String> tokenize(java.lang.String str, java.lang.String delimiter)
- Parameters:
str
- the String to tokenizedelimiter
- the delimiter- Returns:
- the String tokens
- Throws:
java.lang.NullPointerException
- if str is null
-
tokenize
public static java.util.List<java.lang.String> tokenize(java.lang.String str)
Returns a list of tokens delimited by whitespace in String str (useful when handling XML)- Parameters:
str
- the String to tokenize- Returns:
- the String tokens
- Throws:
java.lang.NullPointerException
- if str is null
-
tokenize
public static java.util.Collection<java.lang.String> tokenize(java.lang.String strings, boolean lowercase)
Returns a list of tokens delimited by comma. Useful for queries.- Parameters:
strings
- the String to tokenizelowercase
- if true, set all strings to be lowercase- Returns:
- the String tokens
-
bufferToHexString
public static java.lang.String bufferToHexString(byte[] buffer)
Convert a byte buffer to a hexadecimal string.- Parameters:
buffer
- byte buffer- Returns:
- hexadecimal string
-
split
public static java.lang.String[] split(java.lang.String str, java.lang.String delim)
Returns a list of tokens delimited by delim in String str. eg. split("abc@#def@#", "@#") returns a 3 element array containing "abc", "def" and ""- Parameters:
str
- the String to tokenizedelim
- the delimiter String- Returns:
- the String tokens
- Throws:
java.lang.NullPointerException
- if str or delim is null
-
capitalise
public static java.lang.String capitalise(java.lang.String str)
Returns a capitalised version of the given String- Parameters:
str
- the String to capitalise- Returns:
- the capitalised version of str
-
decapitalise
public static java.lang.String decapitalise(java.lang.String str)
Returns a decapitalised version of the given String unless string is an acronym. Gene to gene Protein to protein MRNA to MRNA CDS to CDS- Parameters:
str
- the String to decapitalise- Returns:
- the decapitalised version of str
-
reverseCapitalisation
public static java.lang.String reverseCapitalisation(java.lang.String str)
Reverses the capitalisation of the first character of the given String.- Parameters:
str
- a String- Returns:
- another String
-
pluralise
public static java.lang.String pluralise(java.lang.String str)
Returns a pluralised version of the given String- Parameters:
str
- the String to pluralize- Returns:
- the pluralised version of str
-
toSameInitialCase
public static java.lang.String toSameInitialCase(java.lang.String n, java.lang.String template)
Returns a string with the same initial letter case as the template string- Parameters:
n
- the String to converttemplate
- the String to base the conversion on- Returns:
- the new String, capitalised like template
-
uniqueString
public static java.lang.String uniqueString()
Returns a String that is different every time- Returns:
- a String that is different every time
-
setNextUniqueNumber
public static void setNextUniqueNumber(long number)
Sets the number that is used to generate the next uniqueString. NOTE: DO NOT USE THIS METHOD, unless you are absolutely sure no other thread is going to go anywhere near StringUtil behind your back. This method is for testing purposes only.- Parameters:
number
- the number to set
-
duplicateQuotes
public static java.lang.String duplicateQuotes(java.lang.String s)
Duplicates single quotes in Strings for correct storage in postgres.- Parameters:
s
- the string to format- Returns:
- the string with duplicated single quotes
-
escapeWithBackslashes
public static java.lang.String escapeWithBackslashes(java.lang.String str)
Escapes single quotes and backslashes, with backslashes.- Parameters:
str
- the string to format- Returns:
- the modified string
-
escapeBackslash
public static java.lang.String escapeBackslash(java.lang.String s)
Escape single backslash with single forwardslash for correct storage in postgres.- Parameters:
s
- the string to format- Returns:
- the string with duplicated double backslash
-
trimLeft
public static java.lang.String trimLeft(java.lang.String s)
trim left space in string- Parameters:
s
- the string to format- Returns:
- the string with no whitespace in the left of string
-
allDigits
public static boolean allDigits(java.lang.String s)
Return true if all characters in a given String are digits. Null or empty string will return false. Ignores negative sign and decimal point.- Parameters:
s
- the string to examine- Returns:
- true if all characters are digits
-
prettyList
public static java.lang.String prettyList(java.util.Collection<java.lang.String> elements)
Take a collection of Strings and return a combined string as a comma separated list with 'and' between the final pair. For example: [a, b, c] to "a, b and c".- Parameters:
elements
- a collection of strings to put in the list.- Returns:
- a string with all the elements suitable for inclusion in a sentence.
-
prettyList
public static java.lang.String prettyList(java.util.Collection<java.lang.String> elements, boolean sort)
Take a collection of Strings and return a combined string as a comma separated list with 'and' between the final pair. For example: [a, b, c] to "a, b and c".- Parameters:
elements
- a collection of strings to put in the list.sort
- if true then order the strings alphabetically- Returns:
- a string with all the elements suitable for inclusion in a sentence.
-
indefiniteArticle
public static java.lang.String indefiniteArticle(java.lang.String s)
Return 'a' or 'an' according to first letter of the given article. If article starts with a vowel or appears to be an acronym return 'an'.- Parameters:
s
- the subject of the article- Returns:
- the appropriate indefinite article
-
serializedSortOrderToMap
public static java.util.Map<java.lang.String,java.lang.String> serializedSortOrderToMap(java.lang.String str)
Make a Map from the serialized String returned by jQuery.sortable("serialize").- Parameters:
str
- the String- Returns:
- a Map
-
colonsToDots
public static java.lang.String colonsToDots(java.lang.String in)
Converts all the colons in a String into dots.- Parameters:
in
- an input String- Returns:
- a new String
-
trimSlashes
public static java.lang.String trimSlashes(java.lang.String s)
Trim starting and trailing '/' characters from a string if present.- Parameters:
s
- the string to trim slashes from- Returns:
- a string with no starting or trailing slashes, or null if input string was null
-
wrapLines
public static StringUtil.LineWrappedString wrapLines(java.lang.String input, int lineLength, int lineCount)
Wraps the given String into several lines and ultimately truncates it with an ellipsis.- Parameters:
input
- the String to shortenlineLength
- the maximum line lengthlineCount
- the maximum number of lines- Returns:
- a formatted String
-
wrapLines
public static StringUtil.LineWrappedString wrapLines(java.lang.String input, int lineLength, int lineCount, int lastLineShorter)
Wraps the given String into several lines and ultimately truncates it with an ellipsis.- Parameters:
input
- the String to shortenlineLength
- the maximum line lengthlineCount
- the maximum number of lineslastLineShorter
- the number of characters by which the last line is shorter- Returns:
- a formatted String
-
-