155 lines
4.3 KiB
Java
155 lines
4.3 KiB
Java
package com.rjconsultores.ventaboletos.utilerias;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.text.DecimalFormat;
|
|
import java.text.DecimalFormatSymbols;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
public class StringHelper {
|
|
|
|
public static final String QUEBRA_LINHA = "\r\n";
|
|
|
|
private static String[] REPLACES = { "a", "e", "i", "o", "u", "c", "A", "E", "I", "O", "U", "C" };
|
|
private static Pattern[] PATTERNS = null;
|
|
|
|
private StringHelper() {
|
|
}
|
|
|
|
private static void compilePatterns() {
|
|
PATTERNS = new Pattern[REPLACES.length];
|
|
PATTERNS[0] = Pattern.compile("[âãáàä]");
|
|
PATTERNS[1] = Pattern.compile("[éèêë]");
|
|
PATTERNS[2] = Pattern.compile("[íìîï]");
|
|
PATTERNS[3] = Pattern.compile("[óòôõö]");
|
|
PATTERNS[4] = Pattern.compile("[úùûü]");
|
|
PATTERNS[5] = Pattern.compile("[ç]");
|
|
PATTERNS[6] = Pattern.compile("[ÂÃÁÀÄ]");
|
|
PATTERNS[7] = Pattern.compile("[ÉÈÊË]");
|
|
PATTERNS[8] = Pattern.compile("[ÍÌÎÏ]");
|
|
PATTERNS[9] = Pattern.compile("[ÓÒÔÕÖ]");
|
|
PATTERNS[10] = Pattern.compile("[ÚÙÛÜ]");
|
|
PATTERNS[11] = Pattern.compile("[Ç]");
|
|
}
|
|
|
|
public static String replaceAcento(String text) {
|
|
if (PATTERNS == null) {
|
|
compilePatterns();
|
|
}
|
|
|
|
String result = text;
|
|
for (int i = 0; i < PATTERNS.length; i++) {
|
|
Matcher matcher = PATTERNS[i].matcher(result);
|
|
result = matcher.replaceAll(REPLACES[i]);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static String retornaSomenteNumeros(String str) {
|
|
if (str != null) {
|
|
return str.replaceAll("[^0123456789]", "");
|
|
} else {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public static String removeStringEsquerda(String str, String strRemover) {
|
|
if (str != null) {
|
|
str = StringUtils.stripStart(str, strRemover);
|
|
return str;
|
|
} else {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public static String preencherStringEspacoDireita(String str, int tamanho) {
|
|
return preencherStringDireita(str, tamanho, StringUtils.EMPTY);
|
|
}
|
|
|
|
public static String preencherStringEspacoEsquerda(String str, int tamanho) {
|
|
return preencherStringEsquerda(str, tamanho, StringUtils.EMPTY);
|
|
}
|
|
|
|
public static String preencherZeroDireita(String str, int tamanho) {
|
|
return preencherStringDireita(str, tamanho, "0");
|
|
}
|
|
|
|
public static String preencherZeroEsquerda(String str, int tamanho) {
|
|
return preencherStringEsquerda(str, tamanho, "0");
|
|
}
|
|
|
|
public static String preencherZeroEsquerda(Integer val, int tamanho) {
|
|
String value = val == null ? "" : val.toString();
|
|
|
|
return preencherStringEsquerda(value, tamanho, "0");
|
|
}
|
|
|
|
public static String preencherStringDireita(String str, int tamanho, String preenchimento) {
|
|
if (str != null) {
|
|
if (str.length() > tamanho) {
|
|
str = str.substring(0, tamanho);
|
|
} else {
|
|
str = StringUtils.rightPad(str, tamanho, preenchimento);
|
|
}
|
|
return str;
|
|
} else {
|
|
return StringUtils.rightPad(StringUtils.EMPTY, tamanho, preenchimento);
|
|
}
|
|
}
|
|
|
|
public static String preencherStringEsquerda(String str, int tamanho, String preenchimento) {
|
|
if (str != null) {
|
|
if (str.length() > tamanho) {
|
|
str = str.substring(0, tamanho);
|
|
} else {
|
|
str = StringUtils.leftPad(str, tamanho, preenchimento);
|
|
}
|
|
return str;
|
|
} else {
|
|
return StringUtils.leftPad(StringUtils.EMPTY, tamanho, preenchimento);
|
|
}
|
|
}
|
|
|
|
public static String preencherPipeDireita(String str, int tamanho) {
|
|
if (str != null && !StringUtils.isEmpty(str)) {
|
|
if (str.length() > tamanho) {
|
|
str = str.substring(0, tamanho);
|
|
}
|
|
str = StringUtils.rightPad(str, str.length() + 1, "|");
|
|
return str;
|
|
} else {
|
|
return "|";
|
|
}
|
|
}
|
|
|
|
public static String preencherPipeDireita(String str) {
|
|
if (str != null && !StringUtils.isEmpty(str)) {
|
|
str = StringUtils.rightPad(str, str.length() + 1, "|");
|
|
return str;
|
|
} else {
|
|
return "|";
|
|
}
|
|
}
|
|
|
|
public static String divideEFormata(String str) {
|
|
|
|
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
|
|
symbols.setDecimalSeparator(',');
|
|
DecimalFormat formatador = new DecimalFormat("0.00", symbols);
|
|
|
|
String valor = "0";
|
|
if (str != null) {
|
|
valor = new BigDecimal(str).divide(new BigDecimal(100)).toString();
|
|
}
|
|
return formatador.format(new BigDecimal(valor));
|
|
|
|
}
|
|
|
|
public static Boolean isNumeric(String str) {
|
|
return StringUtils.isNumeric(str);
|
|
}
|
|
|
|
}
|