
io.lsn.java.common.helper.StringHelper Maven / Gradle / Ivy
package io.lsn.java.common.helper;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Patryk Szlagowski
*/
public class StringHelper {
/**
* Concatenate list values, skip nulls and trim whole string at the end
*
* @param list list of strings
* @param separator separator
* @return
*/
public static String concatWithSeparator(List list, String separator) {
if (list != null) {
return list.stream().filter(value -> value != null).collect(Collectors.joining(separator)).trim();
} else {
return null;
}
}
/**
* Clean string / object from additional spaces
* If there is something more to be cleared/removed - please add it in this function
*
* @param value
* @return
*/
public static String cleanString(String value) {
if (value != null) {
if (value.trim().equals("")) {
return null;
} else {
return value.trim();
}
} else {
return value;
}
}
/**
* Returns string or empty string (if null)
*
* @param value
* @return
*/
public static String notNullString(String value) {
if (value == null) {
return "";
} else {
return value;
}
}
/**
* Match element1 with element2 using provided wildcard
*
* @param element1
* @param element2
* @param wildcard
* @return
*/
public static Boolean equalsWithWildcard(String element1, String element2, char wildcard) {
if (element1 != null && element2 != null) {
if (element1.length() != element2.length()) {
return false;
}
for (int i = 0; i < element1.length(); i += 1) {
if (element1.charAt(i) == wildcard || element2.charAt(i) == wildcard || element1.charAt(i) == element2.charAt(i)) {
continue;
}
return false;
}
return true;
}
return false;
}
public static BigDecimal decimalValue(String dec) {
try {
return BigDecimal.valueOf(Double.valueOf(dec.replace(",", ".")));
} catch (Exception e) {
return null;
}
}
/**
* @param inString
* @return
*/
public static String removeLeadZero(String inString) {
return inString.replaceFirst("^0*", "");
}
/**
* add leading zeros
*
* @param in
* @param length
* @return
*/
public static String addLeadZero(String in, int length) {
if (in.length() >= length) {
return in;
}
return String.format("%0" + (length - in.length()) + "d%s", 0, in);
}
/**
* Returns default value if value is null, or value
*
* @param value
* @param defaultValue
* @return
*/
public static String nvl(String value, String defaultValue) {
if (value == null) {
return defaultValue;
}
return value;
}
/**
* get unique id based on hashcode of instance
*
* @param instance
* @return
*/
public static String getUniqueIdForObject(Object instance) {
String id = String.valueOf(System.identityHashCode(instance));
if (id.length() > 9) {
return id.substring(0, 9);
}
return StringHelper.addLeadZero(id, 9);
}
/**
* get information with class and line
*
* @param message
* @return
*/
public static String debugInfo(String message) {
StackTraceElement element = new Throwable().getStackTrace()[1];
return String.format("%s (%s:%s)", message, element.getClassName(), element.getLineNumber());
}
/**
* returns true if string is null or empty
*
* @param s string to be validated
* @return
*/
public static Boolean isNullOrEmpty(String s) {
return s == null || s.trim().isEmpty();
}
/**
* return substring of provided value starting from index 0;
*
* @param s
* @param length
* @return
*/
public static String cut(String s, int length) {
if (s == null) {
return null;
}
if (length > s.length() - 1) {
return s;
} else {
return s.substring(0, length - 1);
}
}
public static Boolean isNotEmpty(String value) {
return value != null && !value.isEmpty();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy