Package net.sourceforge.pmd.util
Class StringUtil
java.lang.Object
net.sourceforge.pmd.util.StringUtil
String-related utility functions. See also
StringUtils.- Author:
- BrianRemedios, Clément Fournier
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic StringBuilderappend(StringBuilder sb, CharSequence charSeq) LikeStringBuilder.append(CharSequence), but uses an optimized implementation if the charsequence happens to be aChars.static StringFormats all items onto a string with separators if more than one exists, return an empty string if the items are null or empty.static intcolumnNumberAt(CharSequence charSeq, int offsetInclusive) Returns the (1-based) column number of the character at the given index.static StringTruncate the given string to some maximum length.static StringescapeJava(String str) Replaces unprintable characters by their escaped (or unicode escaped) equivalents in the given stringstatic StringReplace some whitespace characters so they are visually apparent.static @NonNull StringinDoubleQuotes(String expected) static Stringstatic booleanAre the two String values the same.static intlineNumberAt(CharSequence charSeq, int offsetInclusive) Returns the (1-based) line number of the character at the given index.linesWithTrimIndent(Chars source) Returns a list ofstatic StringnullToEmpty(String value) Return the empty string if the parameter is null.static StringpercentageString(double val, int numDecimals) Formats a double to a percentage, keepingnumDecimaldecimal places.static StringquoteMessageFormat(String str) Escape the string so that it appears literally when interpreted by aMessageFormat.static StringRemove characters, that are not allowed in XML 1.0 documents.static StringremoveDoubleQuotes(String string) LikeremoveSurroundingwith a double quote as a delimiter.static StringremoveSurrounding(String string, char delimiter) If the string starts and ends with the delimiter, returns the substring within the delimiters.static StringsubstringAfterLast(String str, int c) Returns the substring following the last occurrence of the given character.static StringBuildertrimIndent(Chars string) Trim common indentation in the lines of the string.static voidtrimIndentInPlace(List<Chars> lines) Trim the common indentation of each line in place in the input list.static StringwithoutPrefixes(String text, String... prefixes) Checks for the existence of any of the listed prefixes on the non-null text and removes them.
-
Method Details
-
inSingleQuotes
-
inDoubleQuotes
-
lineNumberAt
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- Parameters:
charSeq- Char sequenceoffsetInclusive- Offset in the sequence of the targeted character. May be the length of the sequence.- Returns:
- -1 if the offset is not in
[0, length], otherwise the line number
-
columnNumberAt
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- Parameters:
charSeq- Char sequenceoffsetInclusive- Offset in the sequence- Returns:
- -1 if the offset is not in
[0, length], otherwise the column number
-
append
LikeStringBuilder.append(CharSequence), but uses an optimized implementation if the charsequence happens to be aChars.StringBuilderalready optimises the cases where the charseq is a string, a StringBuilder, or a stringBuffer. This is especially useful in parsers. -
substringAfterLast
Returns the substring following the last occurrence of the given character. If the character doesn't occur, returns the whole string. This contrasts withStringUtils.substringAfterLast(String, String), which returns the empty string in that case.- Parameters:
str- String to cutc- Delimiter
-
percentageString
Formats a double to a percentage, keepingnumDecimaldecimal places.- Parameters:
val- a double value between 0 and 1numDecimals- The number of decimal places to keep- Returns:
- A formatted string
- Throws:
IllegalArgumentException- if the double to format is not between 0 and 1
-
withoutPrefixes
Checks for the existence of any of the listed prefixes on the non-null text and removes them.- Returns:
- String
-
removedInvalidXml10Characters
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)). -
escapeWhitespace
Replace some whitespace characters so they are visually apparent.- Returns:
- String
-
linesWithTrimIndent
Returns a list of -
trimIndentInPlace
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.- Parameters:
lines- mutable list
-
trimIndent
Trim common indentation in the lines of the string. LiketrimIndentInPlace(List)called with the list of lines and joined with\n. -
isSame
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.- Parameters:
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.- Returns:
trueif the Strings are the same,falseotherwise.
-
asString
Formats all items onto a string with separators if more than one exists, return an empty string if the items are null or empty.- Parameters:
items- Object[]separator- String- Returns:
- String
-
removeSurrounding
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') = "_" -
removeDoubleQuotes
LikeremoveSurroundingwith a double quote as a delimiter. -
elide
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. -
escapeJava
Replaces unprintable characters by their escaped (or unicode escaped) equivalents in the given string -
quoteMessageFormat
Escape the string so that it appears literally when interpreted by aMessageFormat. -
nullToEmpty
Return the empty string if the parameter is null.
-