com.sap.it.commons.tools.StringTools Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of odata-core Show documentation
Show all versions of odata-core Show documentation
SAP Cloud Platform SDK for service development
The newest version!
package com.sap.it.commons.tools;
import java.util.regex.Pattern;
public class StringTools {
private StringTools() {
}
public static final String LINE_SAPARATOR = System.getProperty("line.separator");
public static final Pattern LINE_SAPARATOR_PATTERN = Pattern.compile(LINE_SAPARATOR);
public static final String EMPTY_STRING = "";
public static String[] EMPTY_STRING_ARRAY = new String[0];
public static boolean hasContent(String data) {
return data != null && !data.isEmpty();
}
public static boolean isEmpty(String data) {
return data == null || data.isEmpty();
}
public static String nullify(String data) {
return isEmpty(data) ? null : data;
}
public static String emptyfy(String data) {
return data == null ? EMPTY_STRING : data;
}
public static String trimLeading(String data, String trim) {
if (isEmpty(trim)) {
return data;
}
if (data == null) {
return null;
}
if (data.startsWith(trim)) {
return data.substring(trim.length());
} else {
return data;
}
}
public static String trimTrailing(String data, String trim) {
if (isEmpty(trim)) {
return data;
}
if (data == null) {
return null;
}
if (data.endsWith(trim)) {
return data.substring(0, data.length() - trim.length());
} else {
return data;
}
}
public static String grantPrefix(String data, String prefix) {
if (isEmpty(prefix)) {
return data;
}
if (data == null) {
return prefix;
}
if (data.startsWith(prefix)) {
return data;
} else {
return prefix + data;
}
}
public static String prefix(String data, String prefix) {
if (isEmpty(prefix)) {
return data;
}
if (data == null) {
return prefix;
}
return prefix + data;
}
public static String grantSuffix(String data, String suffix) {
if (isEmpty(suffix)) {
return data;
}
if (data == null) {
return suffix;
}
if (data.endsWith(suffix)) {
return data;
} else {
return data + suffix;
}
}
public static String limit(String data, int maxLength) {
if (data == null || data.length() <= maxLength) {
return data;
}
return data.substring(0, maxLength);
}
public static String serialize(Object object) {
if (object == null) {
return EMPTY_STRING;
}
String res = object.toString();
return res != null ? res : EMPTY_STRING;
}
public static int length(String data) {
return data != null ? data.length() : 0;
}
public static String defaultIfEmpty(String data, String defaultValue) {
return isEmpty(data) ? defaultValue : data;
}
public static String textBefore(String data, char terminal) {
if (isEmpty(data)) {
return data;
}
int pos = data.indexOf(terminal);
if (pos == -1) {
return data;
} else {
return data.substring(0, pos);
}
}
/**
* Method to prevent compiler inlining for Strings
*
* @param data
* @return data
*
* @since 0.1.0
*/
public static String noConst(String data) {
return data;
}
public static String join(Object[] array, String separator, int startIndex, int endIndex) {
if (array == null) {
return null;
}
if (separator == null) {
separator = EMPTY_STRING;
}
// endIndex - startIndex > 0: Len = NofStrings *(len(firstString) + len(separator))
// (Assuming that all Strings are roughly equally long)
int bufSize = (endIndex - startIndex);
if (bufSize <= 0) {
return EMPTY_STRING;
}
bufSize *= ((array[startIndex] == null ? 16 : array[startIndex].toString().length())
+ separator.length());
StringBuilder stringBuilder = new StringBuilder(bufSize);
for (int i = startIndex; i < endIndex; i++) {
if (i > startIndex) {
stringBuilder.append(separator);
}
if (array[i] != null) {
stringBuilder.append(array[i]);
}
}
return stringBuilder.toString();
}
public static String join(Object[] array, String separator) {
if (array == null) {
return null;
}
return join(array, separator, 0, array.length);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy