2019-10-26 23:15:49 +02:00
|
|
|
package fr.pandacube.util;
|
2016-02-16 20:07:51 +01:00
|
|
|
|
2018-07-21 17:57:44 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2016-02-16 20:07:51 +01:00
|
|
|
public class StringUtil {
|
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
|
|
|
/**
|
|
|
|
* @param s Chaine de caractère à parcourir
|
|
|
|
* @param c_match le caractère dont on doit retourner le nombre d'occurence
|
|
|
|
* @return nombre d'occurence de <b>c_match</b> dans <b>s</b>
|
|
|
|
*/
|
2016-07-14 14:22:23 +02:00
|
|
|
public static int char_count(CharSequence s, char c_match) {
|
2016-02-16 20:07:51 +01:00
|
|
|
char[] chars = s.toString().toCharArray();
|
|
|
|
int count = 0;
|
|
|
|
for (char c : chars)
|
2016-07-14 14:22:23 +02:00
|
|
|
if (c == c_match) count++;
|
2016-02-16 20:07:51 +01:00
|
|
|
return count;
|
|
|
|
}
|
2018-07-21 17:57:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String joinGrammatically(CharSequence sep1, CharSequence sepFinal, List<String> strings) {
|
|
|
|
int size = strings == null ? 0 : strings.size();
|
|
|
|
return size == 0 ? "" : size == 1 ? strings.get(0) : String.join(sep1, strings.subList(0, --size)) + sepFinal + strings.get(size);
|
|
|
|
}
|
2016-02-16 20:07:51 +01:00
|
|
|
}
|