Class SplitUtil
Example
// Given some arbitrary input string
String input = "Hello World";
// Try to best-effort split based on the strategy,
// in this case by spaces even if the partial string is not close to the limit
// ["Hello", "World"]
SplitUtil.split(input, 8, true, Strategy.SPACE);
// Cases where the string can fit within the limit, will result in no splitting
// ["Hello World"]
SplitUtil.split(input, 50, true, Strategy.SPACE);
In a more applied use-case, you can also define a smaller limit so it can fit into codeblocks of a message:
public List<String> getRoleNames(Guild guild)
{
// Create a newline separated list of role names from the guild
String roleNames = guild.getRoleCache().applyStream(stream ->
stream.map(Role::getName)
.collect(Collectors.joining("\n"))
);
// Split the role names into a list of strings each small enough to fit into a message codeblock
// A message can be 2000 characters long, do the math (2000 - 7 = 1993 characters) but to be safe go a little lower
List<String> blocks = SplitUtil.split(roleNames, 1990, true, Strategy.NEWLINE, Strategy.ANYWHERE);
// Then wrap each of these blocks into a codeblock for sending
return blocks.stream()
.map(block -> "```\n" + block + "```")
.collect(Collectors.toList());
}
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Function which applies a programmable strategy used to determine a splitting point. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionsplit
(String input, int limit, boolean trim, SplitUtil.Strategy... strategies) Apply a list ofStrategies
to split the provided string into chunks of a maximumlimit
characters.split
(String input, int limit, SplitUtil.Strategy... strategies) Apply a list ofStrategies
to split the provided string into chunks of a maximumlimit
characters.
-
Constructor Details
-
SplitUtil
public SplitUtil()
-
-
Method Details
-
split
@Nonnull public static List<String> split(@Nonnull String input, int limit, @Nonnull SplitUtil.Strategy... strategies) Apply a list ofStrategies
to split the provided string into chunks of a maximumlimit
characters.
The substring chunks will not be trimmed of whitespace, you can usesplit(String, int, boolean, Strategy...)
to trim them.If no strategies are provided, ie.
split(string, limit, true)
, then it only uses the limit to split withSplitUtil.Strategy.ANYWHERE
.Strategies are applied in order, each trying to split with different criteria. When a strategy fails, the next in the list is tried until all strategies are exhausted. If not a single strategy can split the string, an
IllegalStateException
is thrown.- Parameters:
input
- The input string to split uplimit
- The maximum string length for each chunkstrategies
- The split strategies- Returns:
List
of each substring which is at mostlimit
characters long- Throws:
IllegalStateException
- If none of the strategies successfully split the string. You can useSplitUtil.Strategy.ANYWHERE
to always split at the limit and avoid this exception.- See Also:
-
split
@Nonnull public static List<String> split(@Nonnull String input, int limit, boolean trim, @Nonnull SplitUtil.Strategy... strategies) Apply a list ofStrategies
to split the provided string into chunks of a maximumlimit
characters.If no strategies are provided, ie.
split(string, limit, true)
, then it only uses the limit to split withSplitUtil.Strategy.ANYWHERE
.Strategies are applied in order, each trying to split with different criteria. When a strategy fails, the next in the list is tried until all strategies are exhausted. If not a single strategy can split the string, an
IllegalStateException
is thrown.- Parameters:
input
- The input string to split uplimit
- The maximum string length for each chunktrim
- Whether to trim the chunks after splitting (SeeString.trim()
)strategies
- The split strategies- Returns:
List
of each substring which is at mostlimit
characters long- Throws:
IllegalStateException
- If none of the strategies successfully split the string. You can useSplitUtil.Strategy.ANYWHERE
to always split at the limit and avoid this exception.- See Also:
-