public static int lineNumberAt(CharSequence charSeq, int offsetInclusive)
Returns the (1-based) line number of the character at the given index. Line terminators (\r, \n) are assumed to be on the line they *end* and not on the following line. The method also accepts that the given offset be the length of the string (in which case there's no targeted character), to get the line number of a character that would be inserted at the end of the string.
lineNumberAt("a\nb", 0) = 1 lineNumberAt("a\nb", 1) = 1 lineNumberAt("a\nb", 2) = 2 lineNumberAt("a\nb", 3) = 2 // charAt(3) doesn't exist though lineNumberAt("a\nb", 4) = -1 lineNumberAt("", 0) = 1 lineNumberAt("", _) = -1
charSeq
- Char sequence
offsetInclusive
- Offset in the sequence of the targeted character. May be the length of the sequence.
[0, length]
, otherwise the line number
public static int columnNumberAt(CharSequence charSeq, int offsetInclusive)
Returns the (1-based) column number of the character at the given index. Line terminators are by convention taken to be part of the line they end, and not the new line they start. Each character has width 1 (including
\t
). The method also accepts that the given offset be the length of the string (in which case there's no targeted character), to get the column number of a character that would be inserted at the end of the string.
columnNumberAt("a\nb", 0) = 1 columnNumberAt("a\nb", 1) = 2 columnNumberAt("a\nb", 2) = 1 columnNumberAt("a\nb", 3) = 2 // charAt(3) doesn't exist though columnNumberAt("a\nb", 4) = -1 columnNumberAt("a\r\n", 2) = 3
charSeq
- Char sequence
offsetInclusive
- Offset in the sequence
[0, length]
, otherwise the column number
public static StringBuilder append(StringBuilder sb, CharSequence charSeq)
Like
StringBuilder.append(CharSequence)
, but uses an optimized implementation if the charsequence happens to be a
Chars
.
StringBuilder
already optimises the cases where the charseq is a string, a StringBuilder, or a stringBuffer. This is especially useful in parsers.
public static String substringAfterLast(String str, int c)
Returns the substring following the last occurrence of the given character. If the character doesn't occur, returns the whole string. This contrasts with StringUtils.substringAfterLast(String, String)
, which returns the empty string in that case.
str
- String to cut
c
- Delimiter
public static String percentageString(double val, int numDecimals)
Formats a double to a percentage, keeping numDecimal
decimal places.
val
- a double value between 0 and 1
numDecimals
- The number of decimal places to keep
IllegalArgumentException
- if the double to format is not between 0 and 1
public static String withoutPrefixes(String text, String... prefixes)
Checks for the existence of any of the listed prefixes on the non-null text and removes them.
public static String removedInvalidXml10Characters(String text)
Remove characters, that are not allowed in XML 1.0 documents.
Allowed characters are:
Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] // any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
(see
Extensible Markup Language (XML) 1.0 (Fifth Edition)).
public static String escapeWhitespace(Object o)
Replace some whitespace characters so they are visually apparent.
public static void trimIndentInPlace(List<Chars> lines)
Trim the common indentation of each line in place in the input list. Trailing whitespace is removed on each line. Note that blank lines do not count towards computing the max common indentation, except the last one.
lines
- mutable list
public static StringBuilder trimIndent(Chars string)
Trim common indentation in the lines of the string. Like
trimIndentInPlace(List)
called with the list of lines and joined with
\n
.
public static boolean isSame(String s1, String s2, boolean trim, boolean ignoreCase, boolean standardizeWhitespace)
Are the two String values the same. The Strings can be optionally trimmed before checking. The Strings can be optionally compared ignoring case. The Strings can be have embedded whitespace standardized before comparing. Two null values are treated as equal.
s1
- The first String.
s2
- The second String.
trim
- Indicates if the Strings should be trimmed before comparison.
ignoreCase
- Indicates if the case of the Strings should ignored during comparison.
standardizeWhitespace
- Indicates if the embedded whitespace should be standardized before comparison.
true
if the Strings are the same, false
otherwise.
public static String asString(Object[] items, String separator)
Formats all items onto a string with separators if more than one exists, return an empty string if the items are null or empty.
items
- Object[]
separator
- String
public static String removeSurrounding(String string, char delimiter)
If the string starts and ends with the delimiter, returns the substring within the delimiters. Otherwise returns the original string. The start and end delimiter must be 2 separate instances.
removeSurrounding("", _ ) = ""
removeSurrounding("q", 'q') = "q"
removeSurrounding("qq", 'q') = ""
removeSurrounding("q_q", 'q') = "_"
public static String elide(String string, int maxOutputLength, String ellipsis)
Truncate the given string to some maximum length. If it needs truncation, the ellipsis string is appended. The length of the returned string is always lower-or-equal to the maxOutputLength, even when truncation occurs.
public static String escapeJava(String str)
Replaces unprintable characters by their escaped (or unicode escaped) equivalents in the given string
public static String quoteMessageFormat(String str)
Escape the string so that it appears literally when interpreted by a
MessageFormat
.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4