Package org.jsoup.internal
Class StringUtil
java.lang.Object
org.jsoup.internal.StringUtil
public final class StringUtil extends Object
A minimal String utility class. Designed for internal jsoup use only - the API and outcome may change without notice.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A StringJoiner allows incremental / filtered joining of a set of stringable objects. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
appendNormalisedWhitespace
(StringBuilder accum, String string, boolean stripLeading) After normalizing the whitespace within a string, appends it to a string builder.static StringBuilder
Maintains cached StringBuilders in a flyweight pattern, to minimize new StringBuilder GCs.static boolean
in
(String needle, String... haystack) static boolean
inSorted
(String needle, String[] haystack) static boolean
isActuallyWhitespace
(int c) Tests if a code point is "whitespace" as defined by what it looks like.static boolean
isAscii
(String string) Tests that a String contains only ASCII characters.static boolean
isAsciiLetter
(char c) static boolean
isBlank
(String string) Tests if a string is blank: null, empty, or only whitespace (" ", \r\n, \t, etc)static boolean
isDigit
(char c) static boolean
isHexDigit
(char c) static boolean
isInvisibleChar
(int c) static boolean
isNumeric
(String string) Tests if a string is numeric, i.e. contains only ASCII digit charactersstatic boolean
isWhitespace
(int c) Tests if a code point is "whitespace" as defined in the HTML spec.static String
join
(String[] strings, String sep) Join an array of strings by a separatorstatic String
join
(Collection<?> strings, String sep) Join a collection of strings by a separatorstatic String
join
(Iterator<?> strings, String sep) Join a collection of strings by a separatorstatic Collector
<CharSequence, ?, String> joining
(String delimiter) Return aCollector
similar to the one returned byCollectors.joining(
, but backed by jsoup'sCharSequence) StringUtil.StringJoiner
, which allows for more efficient garbage collection.static String
normaliseWhitespace
(String string) Normalise the whitespace within this string; multiple spaces collapse to a single, and all whitespace characters (e.g. newline, tab) convert to a simple space.static String
padding
(int width) Returns space padding (up to the default max of 30).static String
padding
(int width, int maxPaddingWidth) Returns space padding, up to a max of maxPaddingWidth.static String
releaseBuilder
(StringBuilder sb) Release a borrowed builder.static void
releaseBuilderVoid
(StringBuilder sb) Releases a borrowed builder, but does not call .toString() on it.static String
resolve
(String baseUrl, String relUrl) Create a new absolute URL, from a provided existing absolute URL and a relative URL component.static URL
Create a new absolute URL, from a provided existing absolute URL and a relative URL component.static boolean
startsWithNewline
(String string) Tests if a string starts with a newline characterMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
Method Details
-
join
public static String join(Collection<?> strings, String sep) Join a collection of strings by a separator- Parameters:
-
strings
- collection of string objects -
sep
- string to place between strings - Returns:
- joined string
-
join
public static String join(Iterator<?> strings, String sep) Join a collection of strings by a separator- Parameters:
-
strings
- iterator of string objects -
sep
- string to place between strings - Returns:
- joined string
-
join
public static String join(String[] strings, String sep) Join an array of strings by a separator- Parameters:
-
strings
- collection of string objects -
sep
- string to place between strings - Returns:
- joined string
-
padding
public static String padding(int width) Returns space padding (up to the default max of 30). Usepadding(
to specify a different limit.int, int) - Parameters:
-
width
- amount of padding desired - Returns:
- string of spaces * width
- See Also:
-
padding
public static String padding(int width, int maxPaddingWidth) Returns space padding, up to a max of maxPaddingWidth.- Parameters:
-
width
- amount of padding desired -
maxPaddingWidth
- maximum padding to apply. Set to-1
for unlimited. - Returns:
- string of spaces * width
-
isBlank
public static boolean isBlank(String string) Tests if a string is blank: null, empty, or only whitespace (" ", \r\n, \t, etc)- Parameters:
-
string
- string to test - Returns:
- if string is blank
-
startsWithNewline
public static boolean startsWithNewline(String string) Tests if a string starts with a newline character- Parameters:
-
string
- string to test - Returns:
- if its first character is a newline
-
isNumeric
public static boolean isNumeric(String string) Tests if a string is numeric, i.e. contains only ASCII digit characters- Parameters:
-
string
- string to test - Returns:
- true if only digit chars, false if empty or null or contains non-digit chars
-
isWhitespace
public static boolean isWhitespace(int c) Tests if a code point is "whitespace" as defined in the HTML spec. Used for output HTML.- Parameters:
-
c
- code point to test - Returns:
- true if code point is whitespace, false otherwise
- See Also:
-
isActuallyWhitespace
public static boolean isActuallyWhitespace(int c) Tests if a code point is "whitespace" as defined by what it looks like. Used for Element.text etc.- Parameters:
-
c
- code point to test - Returns:
- true if code point is whitespace, false otherwise
-
isInvisibleChar
public static boolean isInvisibleChar(int c) -
normaliseWhitespace
public static String normaliseWhitespace(String string) Normalise the whitespace within this string; multiple spaces collapse to a single, and all whitespace characters (e.g. newline, tab) convert to a simple space.- Parameters:
-
string
- content to normalise - Returns:
- normalised string
-
appendNormalisedWhitespace
public static void appendNormalisedWhitespace(StringBuilder accum, String string, boolean stripLeading) After normalizing the whitespace within a string, appends it to a string builder.- Parameters:
-
accum
- builder to append to -
string
- string to normalize whitespace within -
stripLeading
- set to true if you wish to remove any leading whitespace
-
in
public static boolean in(String needle, String... haystack) -
inSorted
public static boolean inSorted(String needle, String[] haystack) -
isAscii
public static boolean isAscii(String string) Tests that a String contains only ASCII characters.- Parameters:
-
string
- scanned string - Returns:
- true if all characters are in range 0 - 127
-
resolve
Create a new absolute URL, from a provided existing absolute URL and a relative URL component.- Parameters:
-
base
- the existing absolute base URL -
relUrl
- the relative URL to resolve. (If it's already absolute, it will be returned) - Returns:
- the resolved absolute URL
- Throws:
-
MalformedURLException
- if an error occurred generating the URL
-
resolve
public static String resolve(String baseUrl, String relUrl) Create a new absolute URL, from a provided existing absolute URL and a relative URL component.- Parameters:
-
baseUrl
- the existing absolute base URL -
relUrl
- the relative URL to resolve. (If it's already absolute, it will be returned) - Returns:
- an absolute URL if one was able to be generated, or the empty string if not
-
borrowBuilder
public static StringBuilder borrowBuilder()Maintains cached StringBuilders in a flyweight pattern, to minimize new StringBuilder GCs. The StringBuilder is prevented from growing too large.Care must be taken to release the builder once its work has been completed, with
releaseBuilder(
java.lang.StringBuilder) - Returns:
- an empty StringBuilder
-
releaseBuilder
public static String releaseBuilder(StringBuilder sb) Release a borrowed builder. Care must be taken not to use the builder after it has been returned, as its contents may be changed by this method, or by a concurrent thread.- Parameters:
-
sb
- the StringBuilder to release. - Returns:
- the string value of the released String Builder (as an incentive to release it!).
-
releaseBuilderVoid
public static void releaseBuilderVoid(StringBuilder sb) Releases a borrowed builder, but does not call .toString() on it. Useful in case you already have that string.- Parameters:
-
sb
- the StringBuilder to release. - See Also:
-
joining
public static Collector<CharSequence,?, joiningString> (String delimiter) Return aCollector
similar to the one returned byCollectors.joining(
, but backed by jsoup'sCharSequence) StringUtil.StringJoiner
, which allows for more efficient garbage collection.- Parameters:
-
delimiter
- The delimiter for separating the strings. - Returns:
-
A
Collector
which concatenates CharSequence elements, separated by the specified delimiter
-
isAsciiLetter
public static boolean isAsciiLetter(char c) -
isDigit
public static boolean isDigit(char c) -
isHexDigit
public static boolean isHexDigit(char c)
-