1
0
Fork 0
mirror of https://github.com/MatomoCamp/matomocamp-companion-android.git synced 2024-09-19 16:13:46 +02:00

Improved StringUtils.toSlug()

- remove dots
- replace ß with ss
This commit is contained in:
Christophe Beyls 2017-01-29 21:25:58 +01:00
parent 4aa734ca3b
commit b06684a9c5

View file

@ -50,6 +50,20 @@ public class StringUtils {
return new String(result); return new String(result);
} }
public static String remove(String str, final char remove) {
if (TextUtils.isEmpty(str) || str.indexOf(remove) == -1) {
return str;
}
final char[] chars = str.toCharArray();
int pos = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] != remove) {
chars[pos++] = chars[i];
}
}
return new String(chars, 0, pos);
}
/** /**
* Replaces all groups of non-alphanumeric chars in source with a single replacement char. * Replaces all groups of non-alphanumeric chars in source with a single replacement char.
*/ */
@ -99,7 +113,13 @@ public class StringUtils {
* Transforms a name to a slug identifier to be used in a FOSDEM URL. * Transforms a name to a slug identifier to be used in a FOSDEM URL.
*/ */
public static String toSlug(@NonNull String source) { public static String toSlug(@NonNull String source) {
return replaceNonAlphaGroups(trimNonAlpha(removeDiacritics(source)), '_').toLowerCase(Locale.US); source = remove(source, '.');
source = removeDiacritics(source);
source = source.replace("ß", "ss");
source = trimNonAlpha(source);
source = replaceNonAlphaGroups(source, '_');
source = source.toLowerCase(Locale.US);
return source;
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")