Package dk.netarkivet.common.utils
Class StringUtils
- java.lang.Object
-
- dk.netarkivet.common.utils.StringUtils
-
public final class StringUtils extends Object
Utilities for working with strings.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
conjoin(String sep, String... strings)
Concatenate all strings in a collection with the given separator between each.static <T> String
conjoin(String sep, Collection<T> objects)
Concatenate all objects in a collection with the given separator between each.static <T> String
conjoin(String separator, Collection<T> objects, int max)
Concatenate the string representation of a maximum number of objects in a collection with a given separator between them.static String
formatDate(long millis)
Formats the given date (as elapsed milliseconds) using the default format 'yyyy/MM/dd HH:mm:ss'.static String
formatDate(long millis, String format)
Formats the given date (as elapsed milliseconds) using the provided format pattern.static String
formatDuration(long seconds)
Formats a duration in seconds as a string of the form "3d 04:12:56".static String
formatNumber(double number, Locale locale)
Formats a number, as a decimal number with at most 2 digits.static String
formatNumber(long number, Locale locale)
Formats a number, as a decimal number with at most 2 digits.static String
formatPercentage(double percentage)
Formats a numeric percentage, as a decimal number with at most 2 digits.static String
formatPercentage(double percentage, Locale locale)
Formats a numeric percentage, as a decimal number with at most 2 digits.static String
formatPercentage(long percentage)
Formats a numeric percentage, as a decimal number with at most 2 digits.static String
formatPercentage(long percentage, Locale locale)
Formats a numeric percentage, as a decimal number with at most 2 digits.static String
makeEllipsis(String orgString, int maxLength)
Generate a ellipsis of orgString.static List<Integer>
parseIntList(String[] stringArray)
Change all Strings to Integers.static String
repeat(String s, int n)
Repeat the string n times.static String
replace(String sentence, String oldString, String newString)
Replace all occurrences of oldString with newString in a string.static String
splitStringForce(String input, int maxLineLength)
Given a multi-line input string, this method splits the string so that no line has length greater than maxLineLength.static String
splitStringOnWhitespace(String input, int lineLength)
Given an input String, this method splits the String with newlines into a multiline String with line-lengths approximately lineLength.static String
surjoin(List<String> strings, String pre, String post)
Concatenate all strings in a collection, with the fixed strings appended and prepended to each.
-
-
-
Method Detail
-
replace
public static String replace(String sentence, String oldString, String newString)
Replace all occurrences of oldString with newString in a string.- Parameters:
sentence
- the string, where all occurrences of oldString are to be replaced with newString.oldString
- the oldString.newString
- the newString.- Returns:
- the resulting string, where all occurrences of oldString are replaced with newString.
-
conjoin
public static <T> String conjoin(String sep, Collection<T> objects)
Concatenate all objects in a collection with the given separator between each. If the Collection is a List, this method will generate the conjoined string in list order. If the objects are not Strings, the toString method will be used to convert them to strings.- Type Parameters:
T
- The type of objects to conjoin.- Parameters:
sep
- A string to separate the list items.objects
- A collection of object to concatenate as a string.- Returns:
- The concatenated string, or null if the list was null.
-
conjoin
public static <T> String conjoin(String separator, Collection<T> objects, int max) throws ArgumentNotValid
Concatenate the string representation of a maximum number of objects in a collection with a given separator between them. If the Collection is a List, this method will generate the conjoined string in list order. If the objects are not Strings, the toString method will be used to convert them to strings.- Type Parameters:
T
- The type of collection.- Parameters:
separator
- The string to separate the entries in the collection with. This is allowed to be the empty string.objects
- The collection to have the string representation of its entries concatenated.max
- The maximum number of objects in the collection to concatenate. If this number is 0 or below only the first entry in the collection is returned.- Returns:
- The concatenation of the string representation of a limited amount of entries in the collection.
- Throws:
ArgumentNotValid
- If the separator or the objects are null.
-
conjoin
public static String conjoin(String sep, String... strings)
Concatenate all strings in a collection with the given separator between each.- Parameters:
sep
- A string to separate the list items.strings
- An array of strings to concatenate.- Returns:
- The concatenated string, or null if the list was null.
-
surjoin
public static String surjoin(List<String> strings, String pre, String post)
Concatenate all strings in a collection, with the fixed strings appended and prepended to each.- Parameters:
strings
- A list of strings to join up.pre
- A string that will be put in front of each string in the list.post
- A string that will be put after each string in the list.- Returns:
- The joined string, or null if strings is null.
-
repeat
public static String repeat(String s, int n)
Repeat the string n times.- Parameters:
s
- A string to repeat.n
- How many times to repeat it.- Returns:
- A repeated string.
- Throws:
ArgumentNotValid
- if a negative amount is specified.
-
parseIntList
public static List<Integer> parseIntList(String[] stringArray)
Change all Strings to Integers.- Parameters:
stringArray
- the given array of Strings to convert.- Returns:
- a List of Integers.
-
makeEllipsis
public static String makeEllipsis(String orgString, int maxLength)
Generate a ellipsis of orgString. If orgString is longer than maxLength, then we return a String containing the first maxLength characters and then append " ..".- Parameters:
orgString
- the original string.maxLength
- the maximum length of the string before ellipsing it.- Returns:
- an ellipsis of orgString.
-
formatDuration
public static String formatDuration(long seconds)
Formats a duration in seconds as a string of the form "3d 04:12:56".- Parameters:
seconds
- A duration in seconds- Returns:
- a formatted string of the form "3d 04:12:56"
-
formatPercentage
public static String formatPercentage(double percentage)
Formats a numeric percentage, as a decimal number with at most 2 digits.- Parameters:
percentage
- the numeric percentage to format.- Returns:
- a formatted percentage string.
-
formatPercentage
public static String formatPercentage(long percentage)
Formats a numeric percentage, as a decimal number with at most 2 digits.- Parameters:
percentage
- the numeric percentage to format.- Returns:
- a formatted percentage string.
-
formatPercentage
public static String formatPercentage(double percentage, Locale locale)
Formats a numeric percentage, as a decimal number with at most 2 digits.- Parameters:
percentage
- the numeric percentage to format.locale
-- Returns:
- a formatted percentage string.
-
formatPercentage
public static String formatPercentage(long percentage, Locale locale)
Formats a numeric percentage, as a decimal number with at most 2 digits.- Parameters:
percentage
- the numeric percentage to format.locale
-- Returns:
- a formatted percentage string.
-
formatNumber
public static String formatNumber(double number, Locale locale)
Formats a number, as a decimal number with at most 2 digits.- Parameters:
number
- the number to format.locale
-- Returns:
- a formatted number string.
-
formatNumber
public static String formatNumber(long number, Locale locale)
Formats a number, as a decimal number with at most 2 digits.- Parameters:
number
- the number to format.locale
-- Returns:
- a formatted number string.
-
formatDate
public static String formatDate(long millis)
Formats the given date (as elapsed milliseconds) using the default format 'yyyy/MM/dd HH:mm:ss'.- Parameters:
millis
- the date- Returns:
- a formatted date string
-
formatDate
public static String formatDate(long millis, String format)
Formats the given date (as elapsed milliseconds) using the provided format pattern.- Parameters:
millis
- the dateformat
- the format patternSimpleDateFormat
- Returns:
- a formatted date string
-
splitStringOnWhitespace
public static String splitStringOnWhitespace(String input, int lineLength)
Given an input String, this method splits the String with newlines into a multiline String with line-lengths approximately lineLength. The split is made at the first blank space found at more than lineLength characters after the previous split.- Parameters:
input
- the input String.lineLength
- the desired line length.- Returns:
- the split String.
- Throws:
ArgumentNotValid
- if the input is null or the lineLength is not positive
-
splitStringForce
public static String splitStringForce(String input, int maxLineLength)
Given a multi-line input string, this method splits the string so that no line has length greater than maxLineLength. Any input lines less than or equal to this length remain unaffected.- Parameters:
input
- the input String.maxLineLength
- the maximum permitted line length.- Returns:
- the split multi-line String.
- Throws:
ArgumentNotValid
- if input is null or maxLineLength is non-positive
-
-