Class StringUtils
- java.lang.Object
-
- org.apache.commons.lang.StringUtils
-
- com.attivio.util.StringUtils
-
public final class StringUtils extends org.apache.commons.lang.StringUtilsContains utility methods related to strings.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classStringUtils.WhiteSpaceTrimmingInputStreamReplaces any chunk of whitespace characters within a text stream, with a single space character.
-
Field Summary
Fields Modifier and Type Field Description static charBOUNDARY_CHARACTERThe boundary character ("unit separator", 0x1F ASCII) (PLAT-4368)static java.lang.StringUTF8_ENCODINGUTF-8.
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringcompressWhitespace(java.lang.CharSequence s)Compresses whitespace.static java.lang.StringcompressWhitespace(java.lang.String s, boolean keepBoundaryChars)Compresses whitespace.static java.lang.Stringencode(long l)Encodes the valuestatic java.lang.Stringencode(java.lang.String s)Encodessas JSON, escaping Unicodestatic java.lang.Stringencode(java.lang.String value, boolean escapeUnicode)Encodes a value as JSON, with an option to escape Unicodestatic voidescape(java.lang.StringBuilder buffer, java.lang.String value)Writevaluetobuffer, escaping double quote and backslash with a backslash.static intfindMatchingClose(java.lang.String startElem, java.lang.String endElem, java.lang.String text, int startIdx)Finds the matching close (identified byendElem), handling embedded opens (identified bystartElem) and closes.static java.lang.Stringformat(java.lang.String value, java.lang.Object... args)Formatvalueusingargs.static java.util.List<java.lang.String>getDelimitedStringValueList(java.lang.String str, java.lang.String delim, boolean trim, boolean skipEmptyValues)Parses out a string using a delimiter into tokens and returns a string list filled with tokens, if any.static java.lang.StringgetFirst(java.lang.String... vals)Gets the first non-blank value from a list of values, according to org.apache.commons.lang.StringUtils.isBlank().static java.lang.StringgetStackTraceAsString(java.lang.Throwable th)Gets the stack trace for a given throwable as a stringstatic java.util.List<java.lang.String>getTrimmedDelimitedStringValueList(java.lang.CharSequence str, java.lang.String delim)Parses out a string using a delimiter into tokens and returns a string list filled with whitespace-trimmed tokens, if any.static byte[]getUtf8Bytes(java.lang.String s)Gets bytes of string in utf8.static java.lang.StringgetUtf8String(byte[] bytes)Gets string from utf8 bytes.static intindexOfNextLine(java.lang.String str, int startIndex)Starting fromstartIndex, finds the index of the first character of the next line.static booleanisAsciiChar(char c)Tells whether a given character is ASCIIstatic java.lang.Stringjoin(int[] ints, char sep, int count)Joins an array of ints into a string with a given separator.static java.lang.Stringjoin(int[] ints, char sep, int start, int count)Joins an array of ints into a string with a given separator.static java.lang.Stringjoin(int[] ints, java.lang.String sep)Joins an array of ints into a string with a given separator.static java.lang.Stringjoin(int[] ints, java.lang.String sep, int count)Joins an array of ints into a string with a given separator.static java.lang.Stringjoin(int[] ints, java.lang.String sep, int start, int count)Joins an array of ints into a string with a given separator.static java.lang.Stringjoin(long[] longs, java.lang.String sep)Joins an array of longs into a string with a given separator.static java.lang.Stringjoin(java.lang.String[] array, char sep, int startIndex, int endIndex)Join slice ofarrayidentified bystartIndexandendIndexonsep.static java.lang.Stringjoin(java.lang.String a, java.lang.String b, char sep)Join 2 strings on a separator character.static java.lang.Stringjoin(java.util.List<java.lang.String> strList, java.lang.String separator)Joins input string list with separatorstatic intlastIndexOfBefore(java.lang.String src, java.lang.String lastTarget, java.lang.String beforeStr)Finds the last occurrence oflastTargetthat appears before an occurrence ofbeforeStr.static java.lang.StringmillisecondsToString(long time)Converts time in milliseconds to a user-friendlyStringthat lists hours, minutes, seconds, and milliseconds.static byte[]parseIpAddress(java.lang.String address)Parse a string representation of an ipv4 ip address into a byte array.static java.util.Map<java.lang.String,java.lang.String[]>parseQueryString(java.lang.String qs, java.lang.String encoding)Parses a standard uri query string.static java.lang.Stringquote(java.lang.String value)Wrapvaluein double quotes.static voidquote(java.lang.StringBuilder buffer, java.lang.String value)Write the quoted string form ofvaluetobuffer.static java.lang.StringBuilderrepeat(java.lang.StringBuilder buffer, java.lang.String value, int count)Appendvaluetobuffercounttimes.static int[]splitIntArray(java.lang.String data, char sep, int count)Split a String containingcountdecimal integers separated bysep.static java.lang.StringstripOffInvalidXmlCharacters(java.lang.String in, java.lang.String replacement)Ensures that the output String has only valid XML Unicode characters as specified by the XML 1.0 standard.static java.lang.StringstripOffNonAlphanums(java.lang.String str)Removes "bad" (non-alpha-numeric) characters from a String.static java.lang.StringstripOffNonAlphanums(java.lang.String str, java.lang.String okNonAlphaNumChars, java.lang.String replacement)Removes "bad" (non-alpha-numeric) characters from a String.static java.lang.StringstripOffNonAscii(java.lang.String in)Strips off any non-ASCII characters from a stringstatic java.lang.StringtoTitleCase(java.lang.String s)Converts the passed in string to the title case, i.e.static java.lang.StringurlDecode(java.lang.String s)Decode URL in UTF-8.static java.lang.StringurlEncode(java.lang.String s)Encode URL in UTF-8.-
Methods inherited from class org.apache.commons.lang.StringUtils
abbreviate, abbreviate, abbreviateMiddle, capitalise, capitaliseAllWords, capitalize, center, center, center, chomp, chomp, chompLast, chompLast, chop, chopNewline, clean, concatenate, contains, contains, containsAny, containsAny, containsIgnoreCase, containsNone, containsNone, containsOnly, containsOnly, countMatches, defaultIfBlank, defaultIfEmpty, defaultString, defaultString, deleteSpaces, deleteWhitespace, difference, endsWith, endsWithAny, endsWithIgnoreCase, equals, equalsIgnoreCase, escape, getChomp, getCommonPrefix, getLevenshteinDistance, getNestedString, getNestedString, getPrechomp, indexOf, indexOf, indexOf, indexOf, indexOfAny, indexOfAny, indexOfAny, indexOfAnyBut, indexOfAnyBut, indexOfDifference, indexOfDifference, indexOfIgnoreCase, indexOfIgnoreCase, isAllLowerCase, isAllUpperCase, isAlpha, isAlphanumeric, isAlphanumericSpace, isAlphaSpace, isAsciiPrintable, isBlank, isEmpty, isNotBlank, isNotEmpty, isNumeric, isNumericSpace, isWhitespace, join, join, join, join, join, join, join, join, join, lastIndexOf, lastIndexOf, lastIndexOf, lastIndexOf, lastIndexOfAny, lastIndexOfIgnoreCase, lastIndexOfIgnoreCase, lastOrdinalIndexOf, left, leftPad, leftPad, leftPad, length, lowerCase, lowerCase, mid, normalizeSpace, ordinalIndexOf, overlay, overlayString, prechomp, remove, remove, removeEnd, removeEndIgnoreCase, removeStart, removeStartIgnoreCase, repeat, repeat, replace, replace, replaceChars, replaceChars, replaceEach, replaceEachRepeatedly, replaceOnce, reverse, reverseDelimited, reverseDelimitedString, right, rightPad, rightPad, rightPad, split, split, split, split, splitByCharacterType, splitByCharacterTypeCamelCase, splitByWholeSeparator, splitByWholeSeparator, splitByWholeSeparatorPreserveAllTokens, splitByWholeSeparatorPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, splitPreserveAllTokens, startsWith, startsWithAny, startsWithIgnoreCase, strip, strip, stripAll, stripAll, stripEnd, stripStart, stripToEmpty, stripToNull, substring, substring, substringAfter, substringAfterLast, substringBefore, substringBeforeLast, substringBetween, substringBetween, substringsBetween, swapCase, trim, trimToEmpty, trimToNull, uncapitalise, uncapitalize, upperCase, upperCase
-
-
-
-
Field Detail
-
UTF8_ENCODING
public static final java.lang.String UTF8_ENCODING
UTF-8.- See Also:
- Constant Field Values
-
BOUNDARY_CHARACTER
public static final char BOUNDARY_CHARACTER
The boundary character ("unit separator", 0x1F ASCII) (PLAT-4368)- See Also:
- Constant Field Values
-
-
Method Detail
-
format
public static java.lang.String format(java.lang.String value, java.lang.Object... args)Formatvalueusingargs.Returns
nullifvalueis null.
-
stripOffInvalidXmlCharacters
public static java.lang.String stripOffInvalidXmlCharacters(java.lang.String in, java.lang.String replacement)Ensures that the output String has only valid XML Unicode characters as specified by the XML 1.0 standard. Replaces any invalid characters with the input replacement string; if the replacement string is null, the invalid characters are simply removed.- Parameters:
in- the String whose non-valid characters we want to remove.- Returns:
- the input String, stripped of invalid characters, or an empty string if the input is null or empty
-
stripOffNonAlphanums
public static java.lang.String stripOffNonAlphanums(java.lang.String str)
Removes "bad" (non-alpha-numeric) characters from a String. All UTF-16 alpha-numeric characters are retained. For example:stripOffNonAlphanums("ab$*^ab#%##ab", "%.,")yields "ababab".For historical reasons, the underscore character
_is currently not stripped. This will likely change.- Parameters:
str- the String to process- Returns:
- the resulting String
- See Also:
stripOffNonAlphanums(String,String,String)
-
stripOffNonAlphanums
public static java.lang.String stripOffNonAlphanums(java.lang.String str, java.lang.String okNonAlphaNumChars, java.lang.String replacement)Removes "bad" (non-alpha-numeric) characters from a String. All UTF-16 alpha-numeric characters are retained. Replaces non-alphanumeric characters with the passed in replacement string. If the replacement string is null or empty, the bad characters are removed without replacement. For example:stripOffNonAlphanums("ab$*^ab#%##ab", "%.,", "_")yields "ab_ab_%_ab". This version of the method takes an okNonAlphaNumChars argument which allows the caller to keep some non-alphanumerics in the resulting string. A series of non-ok characters will be replaced by a single replacement character.For historical reasons, the underscore character
_is currently not stripped. This will likely change, so we recommend you include underscore inokNonAlphaNumCharsif that's the behavior you want.- Parameters:
str- the String to processokNonAlphaNumChars- the "OK" charactersreplacement- the replacement- Returns:
- the resulting String
-
getTrimmedDelimitedStringValueList
public static java.util.List<java.lang.String> getTrimmedDelimitedStringValueList(java.lang.CharSequence str, java.lang.String delim)Parses out a string using a delimiter into tokens and returns a string list filled with whitespace-trimmed tokens, if any. Null values are not returned (that is, if two delimiters are separated by only whitespace, the null result is skipped).- Parameters:
str- the string to parsedelim- the delimiter- Returns:
- string list filled with trimmed tokens or an empty list if none found
-
getDelimitedStringValueList
public static java.util.List<java.lang.String> getDelimitedStringValueList(java.lang.String str, java.lang.String delim, boolean trim, boolean skipEmptyValues)Parses out a string using a delimiter into tokens and returns a string list filled with tokens, if any. Completely null values (i.e., no characters between delimiters) are always skipped.- Parameters:
str- the string to parsedelim- the delimitertrim- whether to trim the leading and trailing whitespace off each tokenskipEmptyValues- whether to exclude values that are only whitespace. Remember that completely null values are always skipped regardless of the value of this parameter.- Returns:
- string list filled with tokens or an empty list if none found
-
indexOfNextLine
public static int indexOfNextLine(java.lang.String str, int startIndex)Starting fromstartIndex, finds the index of the first character of the next line. Blank lines are skipped. The characters\rand\ndefine new lines. A line separator is considered to belong to the string after it, soindexOfNextLine("one\rtwo\nthree",3)returns 8 (the beginning of 'three'), not 4 (the beginning of 'two').- Parameters:
str- the stringstartIndex- the start index- Returns:
- the index of the next line or -1 if no more lines
-
getStackTraceAsString
public static java.lang.String getStackTraceAsString(java.lang.Throwable th)
Gets the stack trace for a given throwable as a string- Parameters:
th- the throwable- Returns:
- the stack trace as a string
-
compressWhitespace
public static java.lang.String compressWhitespace(java.lang.CharSequence s)
Compresses whitespace. This uses Java'sCharacter.isWhitespacemethod so it specifically does not include non-breaking spaces.- Parameters:
s- String to compress- Returns:
- string with leading and trailing whitespace removed, and internal runs of whitespace replaced by a single space character
-
compressWhitespace
public static java.lang.String compressWhitespace(java.lang.String s, boolean keepBoundaryChars) throws java.io.IOExceptionCompresses whitespace.- Parameters:
s- String to compresskeepBoundaryChars- if true, any boundary characters are retained, otherwise treated as whitespace and compressed- Returns:
- string with leading and trailing whitespace removed, and internal runs of whitespace replaced by a single space character
- Throws:
java.io.IOException
-
toTitleCase
public static java.lang.String toTitleCase(java.lang.String s)
Converts the passed in string to the title case, i.e. capitalizes all the whitespace separated words in the string.- Parameters:
s- the string to convert- Returns:
- the capitalized string
-
stripOffNonAscii
public static java.lang.String stripOffNonAscii(java.lang.String in)
Strips off any non-ASCII characters from a string- Parameters:
in- the string- Returns:
- the resulting string
-
isAsciiChar
public static boolean isAsciiChar(char c)
Tells whether a given character is ASCII- Parameters:
c- the character to test- Returns:
- true if the character is ASCII
-
findMatchingClose
public static int findMatchingClose(java.lang.String startElem, java.lang.String endElem, java.lang.String text, int startIdx)Finds the matching close (identified byendElem), handling embedded opens (identified bystartElem) and closes.startIdxis where the starting elem is located. If 0, it will be searched for in text.- Parameters:
startElem-endElem-text-startIdx-
-
lastIndexOfBefore
public static int lastIndexOfBefore(java.lang.String src, java.lang.String lastTarget, java.lang.String beforeStr)Finds the last occurrence oflastTargetthat appears before an occurrence ofbeforeStr.- Parameters:
src-lastTarget-beforeStr-
-
join
public static java.lang.String join(java.lang.String a, java.lang.String b, char sep)Join 2 strings on a separator character.
-
join
public static java.lang.String join(java.lang.String[] array, char sep, int startIndex, int endIndex)Join slice ofarrayidentified bystartIndexandendIndexonsep. ThrowsNegativeArraySizeExceptionifstartIndexis less than or equal toendIndex.
-
join
public static java.lang.String join(int[] ints, java.lang.String sep)Joins an array of ints into a string with a given separator.- Parameters:
ints- the integerssep- the separator- Returns:
- the joined string
-
join
public static java.lang.String join(long[] longs, java.lang.String sep)Joins an array of longs into a string with a given separator.- Parameters:
longs- the longssep- the separator- Returns:
- the joined string
-
join
public static java.lang.String join(int[] ints, java.lang.String sep, int count)Joins an array of ints into a string with a given separator.- Parameters:
ints- the integerssep- the separator- Returns:
- the joined string
-
join
public static java.lang.String join(int[] ints, java.lang.String sep, int start, int count)Joins an array of ints into a string with a given separator.- Parameters:
ints- the integerssep- the separatorcount- only join this many ints- Returns:
- the joined string
-
join
public static java.lang.String join(int[] ints, char sep, int count)Joins an array of ints into a string with a given separator.- Parameters:
ints- the integerssep- the separatorcount- only join this many ints- Returns:
- the joined string
-
join
public static java.lang.String join(int[] ints, char sep, int start, int count)Joins an array of ints into a string with a given separator.- Parameters:
ints- the integerssep- the separatorcount- only join this many ints- Returns:
- the joined string
-
join
public static java.lang.String join(java.util.List<java.lang.String> strList, java.lang.String separator)Joins input string list with separator- Parameters:
strList- string list to joinseparator- to use in between values- Returns:
- strings joined by separator, or a zero length string if strList is null or empty
-
splitIntArray
public static int[] splitIntArray(java.lang.String data, char sep, int count)Split a String containingcountdecimal integers separated bysep.CAUTION:
datais expected to be properly formatted and only contain characters '0' - '9', ',' and '-'.
-
getUtf8Bytes
public static byte[] getUtf8Bytes(java.lang.String s)
Gets bytes of string in utf8.
-
getUtf8String
public static java.lang.String getUtf8String(byte[] bytes)
Gets string from utf8 bytes.
-
urlDecode
public static java.lang.String urlDecode(java.lang.String s)
Decode URL in UTF-8.
-
urlEncode
public static java.lang.String urlEncode(java.lang.String s)
Encode URL in UTF-8.
-
parseIpAddress
public static byte[] parseIpAddress(java.lang.String address)
Parse a string representation of an ipv4 ip address into a byte array.
-
parseQueryString
public static java.util.Map<java.lang.String,java.lang.String[]> parseQueryString(java.lang.String qs, java.lang.String encoding) throws java.io.UnsupportedEncodingExceptionParses a standard uri query string. Handles null query strings.- Parameters:
qs-encoding-- Returns:
- a map of query parameter to values. guaranteed to return non-null.
- Throws:
java.io.UnsupportedEncodingException
-
encode
public static java.lang.String encode(long l)
Encodes the value- Parameters:
l-- Returns:
- the encoded value
-
encode
public static java.lang.String encode(java.lang.String s)
Encodessas JSON, escaping Unicode- Parameters:
s-- Returns:
- the encoded value
-
encode
public static java.lang.String encode(java.lang.String value, boolean escapeUnicode)Encodes a value as JSON, with an option to escape Unicode- Parameters:
value- the value to encodeescapeUnicode- the escape Unicode option- Returns:
- the encoded string
-
quote
public static java.lang.String quote(java.lang.String value)
Wrapvaluein double quotes.Any double quotes in
valuewill be escaped by a backslash. Any backslashes invaluewill be escaped as double backslashes. No other escaping is done; for example a tab character is returned as simply a tab character, not backslash-t.
-
quote
public static void quote(java.lang.StringBuilder buffer, java.lang.String value)Write the quoted string form ofvaluetobuffer.Any double quotes and backslashes in
valuewill be escaped by a backslash. No other escaping is done; for example a tab character is returned as simply a tab character, not backslash-t.
-
escape
public static void escape(java.lang.StringBuilder buffer, java.lang.String value)Writevaluetobuffer, escaping double quote and backslash with a backslash.
-
repeat
public static java.lang.StringBuilder repeat(java.lang.StringBuilder buffer, java.lang.String value, int count)Appendvaluetobuffercounttimes. A negative value ofcountis silently treated as zero.
-
millisecondsToString
public static java.lang.String millisecondsToString(long time)
Converts time in milliseconds to a user-friendlyStringthat lists hours, minutes, seconds, and milliseconds.- Parameters:
time- the time in milliseconds.- Returns:
- the user-friendly string
-
getFirst
public static java.lang.String getFirst(java.lang.String... vals)
Gets the first non-blank value from a list of values, according to org.apache.commons.lang.StringUtils.isBlank(). Returns null if no non-blank values are found.
-
-