com.softicar.platform.common.core.string.Strings Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.core.string;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* Common utility methods for {@link String}.
*
* @author Oliver Richers
*/
public abstract class Strings {
/**
* Reverses the given {@link String}.
*
* @param string
* the {@link String} to reverse (never null)
* @return the reversed {@link String} (never null)
*/
public static String reversed(String string) {
return new StringBuilder(string).reverse().toString();
}
/**
* Same as {@link String#substring} but clamps the indexes into their valid
* range.
*
* @param string
* the {@link String} to get the substring from (never
* null)
* @param begin
* the begin index (inclusive)
* @param end
* the end index (exclusive)
* @return the substring (never null)
*/
public static String substringClamped(String string, int begin, int end) {
if (begin < string.length() && end > 0 && begin < end) {
begin = Math.max(begin, 0);
end = Math.min(end, string.length());
return string.substring(begin, end);
} else {
return "";
}
}
/**
* Splits a {@link String} using {@link StringTokenizer}.
*
* @param string
* the {@link String} to split (never null)
* @param delimiter
* the delimiter {@link String} (never null and not empty)
* @return the list of substrings
*/
public static List tokenize(String string, String delimiter) {
if (delimiter.isEmpty()) {
throw new IllegalArgumentException("Delimiter must not be empty.");
}
var result = new ArrayList();
var tokenizer = new StringTokenizer(string, delimiter);
while (tokenizer.hasMoreTokens()) {
result.add(tokenizer.nextToken());
}
return result;
}
}