public class StringUtilities extends Object
| Modifier and Type | Method and Description |
|---|---|
static String |
bytesToString(long bytes)
Format a byte count to be human friendly.
|
static String |
commaFormat(long value)
Formats the given number with commas every 3rd digit.
|
static String |
createString(char[] characters)
Creates a new String from a character array by
calling the String package private constructor:
String.String(int, int, char[])
which shares the char[] value array for speed and lowers memory usage since we
don't need to have 2 copies of the char[] in memory at once. |
static String |
createString(int offset,
int length,
char[] characters)
Creates a new String from a character array by
calling the String package private constructor:
String.String(int,int,char[])
which shares the char[] value array for speed and lowers memory usage since we
don't need to have 2 copies of the char[] in memory at once. |
static String |
delete(String stringToDeleteCharacterFrom,
char charToDelete)
Creates a new string identical to stringToDeleteCharacterFrom except with all instances of the given character removed.
|
static String |
extract(StringBuilder builder)
Extracts the result of a StringBuilder using less memory than StringBuilder.toString().
|
static String |
formatDateNicely(Date date,
boolean displayRelativeTimes)
Nicely formats a Date in the Geneious style.
|
static String |
formatElement(org.jdom.Element element)
Prints an Element in pretty format (
Format.getPrettyFormat()) to a String and returns that String. |
static String |
formatElementPreserveText(org.jdom.Element element)
Prints an Element in pretty format similar to (
Format.getPrettyFormat()) and returns that String. |
static String |
formatElementRaw(org.jdom.Element element)
Prints an Element in raw format (
Format.getRawFormat()) to a String and returns that String. |
static String |
formatStackTrace(Throwable throwable)
Prints the stacktrace associated with a Throwable to a String and returns the String.
|
static String |
getTextFromInputStream(InputStream stream,
boolean closeInputStream)
Reads an InputStream into a string until the end of the stream is reached.
|
static String |
getTextFromReader(Reader reader,
boolean closeReader)
Reads a reader into a string until the end of the stream is reached.
|
static String |
humanJoin(Iterable values)
Like join(", ", values), except that the last value is separated with " and " instead of ", ".
|
static String |
join(String separator,
Iterable values)
Concatenates values (calling toString() on each of them), interleaved with the
separator string.
|
static String |
listToString(List<String> list)
Converts a list of Strings into a single String which can be converted back to a list via
stringToList(String) |
static org.jdom.Element |
parseElement(String elementText)
Parses some XML.
|
static org.jdom.Element |
parseElementPreserveText(String elementText)
Parses some XML text which was generated using
formatElementPreserveText(org.jdom.Element). |
static List<String> |
splitIntoLines(String s)
Splits a String into the lines it consists of, with any of the standard newlines
(\r, \n or \r\n) being accepted (as by a BufferedReader).
|
static List<String> |
stringToList(String string)
Converts a String created using
listToString(java.util.List) back into a list of Strings. |
static String |
stripHtmlTags(String string,
boolean onlyIfStartsWithHtmlTag)
Strips all html tags and their contents from the provided String
|
static String |
trimToLength(String string,
int maximumLength,
boolean appendDotsIfTruncated)
Trims this string so that it is at most maximumLength characters long.
|
public static String extract(StringBuilder builder)
StringBuilder.toString() method
is used.builder - the StringBuilder from which to extract the String.
This StringBuilder must have capacity equal to its length to avoid excessive memory usage problems.
The contents of this StringBuilder will be cleared before this method returns.IllegalArgumentException - if the StringBuilder's capacity is not the same as its length.public static String createString(char[] characters)
String.String(int, int, char[])
which shares the char[] value array for speed and lowers memory usage since we
don't need to have 2 copies of the char[] in memory at once. The created String
is equal in length the supplied character array.
WARNING: take extreme care when using this method, anything that uses this should
not modify Mcharacters afterwards and should probably immediately discard any reference to it.characters - the characters to create the String from.createString(int, int, char[])public static String createString(int offset, int length, char[] characters)
String.String(int,int,char[])
which shares the char[] value array for speed and lowers memory usage since we
don't need to have 2 copies of the char[] in memory at once.
WARNING: take extreme care when using this method, anything that uses this should
not modify characters afterwards and should probably immediately discard any reference to it.characters - the characters to create the String from.offset - an offset into the character array to being the String fromlength - the length of the String to create.createString(char[])public static String join(String separator, Iterable values)
separator - String to put between the valuesvalues - Values to concatenate, interleaved with the separatorpublic static String trimToLength(String string, int maximumLength, boolean appendDotsIfTruncated)
If it exceeds this length and appendDotsIfTruncated==false then the string returned is just truncated to maximumLength.
If it exceeds this length and appendDotsIfTruncated==true then the string returned is just truncated to maximumLength-3 and "..." is appended.string - the string to possible trimmaximumLength - the maximum length of the returned string.appendDotsIfTruncated - true to append "..." to the string if it is truncated.IllegalArgumentException - if maximumLength<3 and appendDotsIfTruncated==truepublic static String humanJoin(Iterable values)
values - Values with a meaningful toString() method, to list in a human readable format.public static List<String> splitIntoLines(String s)
s - A stringpublic static String formatStackTrace(Throwable throwable)
java.io.IOException: exception reason at com.biomatters.geneious.publicapi.utilities.GeneralUtilitiesTest.testStackTrace(GeneralUtilitiesTest.java:112) at ...
throwable - the throwable to get a stracktrace for.public static String formatDateNicely(Date date, boolean displayRelativeTimes)
date - the Date to formatdisplayRelativeTimes - if true, will display a time as relative to today when appropriate. For example "Yesterday at 13:21:02" or "Thursday at 10:00:00".
Otherwise, will always display the date in the standard geneious formatpublic static String formatElement(org.jdom.Element element)
Format.getPrettyFormat()) to a String and returns that String.element - the element to print to a String. May be null, in which case "(null)" is returned.formatElementPreserveText(org.jdom.Element),
formatElementRaw(org.jdom.Element)public static String formatElementRaw(org.jdom.Element element)
Format.getRawFormat()) to a String and returns that String.element - the element to print to a String. May be null, in which case "(null)" is returned.formatElement(org.jdom.Element),
formatElementPreserveText(org.jdom.Element)public static String formatElementPreserveText(org.jdom.Element element)
Format.getPrettyFormat()) and returns that String.
Unlike formatElement(org.jdom.Element) this method does not trim whitespace from the start and end of text elements.
When parsing text generated by this method, parseElementPreserveText(String) should be used so that whitespace produced
for pretty formatting does not appear during parsing.
element - the element to print to a String. May be null, in which case "(null)" is returned.formatElementPreserveText(org.jdom.Element),
formatElementRaw(org.jdom.Element),
parseElementPreserveText(String)public static org.jdom.Element parseElement(String elementText) throws org.jdom.JDOMException
elementText - a String representation of the XML (for example returned from formatElement(org.jdom.Element)).org.jdom.JDOMException - if it cannot be parsed.public static org.jdom.Element parseElementPreserveText(String elementText) throws org.jdom.JDOMException
formatElementPreserveText(org.jdom.Element).
Text elements with siblings have their whitespace trimmed.elementText - a String representation of the XML returned from formatElementPreserveText(org.jdom.Element)).org.jdom.JDOMException - if it cannot be parsed.formatElementPreserveText(org.jdom.Element),
parseElement(String)public static String getTextFromInputStream(InputStream stream, boolean closeInputStream) throws IOException
stream - the stream to read from.closeInputStream - true to close the stream when complete or when an exception occurs, false to leave it openIOException - if any method calls to the InputStream throw an IOExceptionFileUtilities.getTextFromFile(java.io.File),
getTextFromReader(java.io.Reader, boolean)public static String getTextFromReader(Reader reader, boolean closeReader) throws IOException
reader - the reader to read fromcloseReader - true to close the reader when complete or when an exception occurs, false to leave it openIOException - if any method calls to the Reader throw an IOException, or the file has length greater than Integer.MAX_VALUEFileUtilities.getTextFromFile(java.io.File),
getTextFromInputStream(java.io.InputStream,boolean)public static String commaFormat(long value)
String.format("%,d",value) but
this method is about 25 times faster.value - the value to formatpublic static String bytesToString(long bytes)
bytes - byte count to formatpublic static String delete(String stringToDeleteCharacterFrom, char charToDelete)
String.relacestringToDeleteCharacterFrom - the string to remove occurances fromcharToDelete - the character to remove all occurances ofpublic static String listToString(List<String> list)
stringToList(String)list - the list to convert to a StringstringToList(String)public static List<String> stringToList(String string)
listToString(java.util.List) back into a list of Strings.string - a string returned from listToString(java.util.List)listToString(java.util.List)public static String stripHtmlTags(String string, boolean onlyIfStartsWithHtmlTag)
string - the string to strip html tags fromonlyIfStartsWithHtmlTag - if this is true, then only if the string starts with an html tag (<html;>) will any tags be stripped