2022-07-20 13:18:57 +02:00
|
|
|
package fr.pandacube.lib.util;
|
2016-02-16 20:07:51 +01:00
|
|
|
|
2021-08-15 03:24:56 +02:00
|
|
|
import java.util.Arrays;
|
2018-07-21 17:57:44 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Provides various methods to manipulate Strings.
|
|
|
|
*/
|
2016-02-16 20:07:51 +01:00
|
|
|
public class StringUtil {
|
2022-07-28 01:13:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the provided double, omitting the decimal part if the provided double is strictly equals to a long value.
|
|
|
|
* @param d the value to convert to string.
|
|
|
|
* @return a string representation of the double value.
|
|
|
|
*/
|
2016-07-14 14:22:23 +02:00
|
|
|
public static String formatDouble(double d) {
|
2016-10-17 00:03:04 +02:00
|
|
|
if (d == (long) d)
|
|
|
|
return String.format("%d", (long) d);
|
|
|
|
return String.valueOf(d);
|
2016-02-16 20:07:51 +01:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2016-02-16 20:07:51 +01:00
|
|
|
/**
|
2022-07-28 01:13:35 +02:00
|
|
|
* Counts the number of occurence of a speficied character in a string.
|
|
|
|
* @param string the character sequence to search into.
|
|
|
|
* @param character the character to count.
|
|
|
|
* @return the number of occurence of
|
|
|
|
* @deprecated Because it uses snake_case naming convention. Use {@link #countOccurences(CharSequence, char)} instead.
|
|
|
|
*/
|
|
|
|
@Deprecated(forRemoval = true, since = "2022-07-26")
|
|
|
|
public static int char_count(CharSequence string, char character) {
|
|
|
|
return countOccurences(string, character);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Counts the number of occurence of a speficied character in a string.
|
|
|
|
* @param string the character sequence to search into.
|
|
|
|
* @param character the character to count.
|
|
|
|
* @return the number of occurence of
|
2016-02-16 20:07:51 +01:00
|
|
|
*/
|
2022-07-28 01:13:35 +02:00
|
|
|
public static int countOccurences(CharSequence string, char character) {
|
2016-02-16 20:07:51 +01:00
|
|
|
int count = 0;
|
2022-07-28 01:13:35 +02:00
|
|
|
for (char c : string.toString().toCharArray()) {
|
|
|
|
if (c == character) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
2016-02-16 20:07:51 +01:00
|
|
|
return count;
|
|
|
|
}
|
2022-07-28 01:13:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do like {@link String#join(CharSequence, Iterable)}, but the last separator is different than the others.
|
|
|
|
* It is usedful when enumerating thins in a sentense, for instance : <code>"a thing<u>, </u>a thing<u> and </u>a thing"</code>
|
|
|
|
* (the coma being the usual separator, and {@code " and "} being the final separator).
|
|
|
|
* @param regularSeparator the separator used everywhere except between the two last strings to join.
|
|
|
|
* @param finalSeparator the separator used between the two last strings to join.
|
|
|
|
* @param strings the strings to join.
|
|
|
|
* @return a new string will all the provided {@code strings} joined using the separators.
|
|
|
|
*/
|
|
|
|
public static String joinGrammatically(CharSequence regularSeparator, CharSequence finalSeparator, List<String> strings) {
|
2018-07-21 17:57:44 +02:00
|
|
|
int size = strings == null ? 0 : strings.size();
|
2022-07-28 01:13:35 +02:00
|
|
|
return switch (size) {
|
|
|
|
case 0 -> "";
|
|
|
|
case 1 -> strings.get(0);
|
|
|
|
default -> String.join(regularSeparator, strings.subList(0, --size)) + finalSeparator + strings.get(size);
|
|
|
|
};
|
2018-07-21 17:57:44 +02:00
|
|
|
}
|
2022-07-28 01:13:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a {@link String} that repeats the base character n times.
|
|
|
|
* @param base the base character.
|
|
|
|
* @param n the number of repetition.
|
|
|
|
* @return a {@link String} that repeats the base character n times.
|
|
|
|
*/
|
|
|
|
public static String repeat(char base, int n) {
|
|
|
|
char[] chars = new char[n];
|
2021-08-15 03:24:56 +02:00
|
|
|
Arrays.fill(chars, base);
|
2022-07-28 01:13:35 +02:00
|
|
|
return String.valueOf(chars);
|
2020-04-06 00:10:14 +02:00
|
|
|
}
|
2016-02-16 20:07:51 +01:00
|
|
|
}
|