Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 795 | blopes | 1 | package br.com.kronus.core; |
| 2 | |||
| 3 | import java.text.DecimalFormat; |
||
| 4 | import java.text.Normalizer; |
||
| 5 | import java.util.regex.Matcher; |
||
| 6 | import java.util.regex.Pattern; |
||
| 7 | |||
| 8 | import org.apache.commons.lang3.StringUtils; |
||
| 9 | |||
| 10 | public class StringUtil { |
||
| 11 | |||
| 12 | public static String setarUpperCase(String campo) { |
||
| 13 | return VerificadorUtil.naoEstaNulo(campo)? campo.toUpperCase() : campo; |
||
| 14 | } |
||
| 15 | |||
| 16 | public static String setarLowerCase(String campo) { |
||
| 17 | return VerificadorUtil.naoEstaNulo(campo)? campo.toLowerCase() : campo; |
||
| 18 | } |
||
| 19 | |||
| 20 | public static String setarUpperCaseComTrim(String campo) { |
||
| 21 | return VerificadorUtil.naoEstaNulo(campo)? campo.toUpperCase().trim() : campo; |
||
| 22 | } |
||
| 23 | |||
| 24 | public static String setarValorAhEsquerdaAteCompletarAhQuantidadeDeCaracteres(String string, String valor, Integer quantidadeCaracteres) { |
||
| 25 | StringBuilder retorno = new StringBuilder(); |
||
| 26 | while (retorno.length() + string.length() < quantidadeCaracteres) { |
||
| 27 | retorno.append(valor); |
||
| 28 | } |
||
| 29 | retorno.append(string); |
||
| 30 | return retorno.toString(); |
||
| 31 | } |
||
| 32 | |||
| 33 | public static String formatarCpf(String cpfSemFormatacao) { |
||
| 34 | String cpfComFormatacao = null; |
||
| 35 | Pattern pattern = Pattern.compile("(\\d{3})(\\d{3})(\\d{3})(\\d{2})"); |
||
| 36 | Matcher matcher = pattern.matcher(cpfSemFormatacao); |
||
| 37 | if (matcher.matches()) { |
||
| 38 | cpfComFormatacao = matcher.replaceAll("$1.$2.$3-$4"); |
||
| 39 | } |
||
| 40 | return cpfComFormatacao; |
||
| 41 | } |
||
| 42 | |||
| 43 | public static String formatarCnpj(String cnpjSemFormatacao) { |
||
| 44 | String cnpjComFormatacao = null; |
||
| 45 | Pattern pattern = Pattern.compile("(\\d{2})(\\d{3})(\\d{3})(\\d{4})(\\d{2})"); |
||
| 46 | Matcher matcher = pattern.matcher(cnpjSemFormatacao); |
||
| 47 | if (matcher.matches()) { |
||
| 48 | // cnpjComFormatacao = matcher.replaceAll("$1.$2.$3-$4"); |
||
| 49 | cnpjComFormatacao = matcher.replaceAll("$1.$2.$3/$4-$5"); |
||
| 50 | } |
||
| 51 | return cnpjComFormatacao; |
||
| 52 | } |
||
| 53 | |||
| 54 | public static String removerAcentos(String str) { |
||
| 55 | str = Normalizer.normalize(str, Normalizer.Form.NFD); |
||
| 56 | str = str.replaceAll("[^\\p{ASCII}]", ""); |
||
| 57 | return str; |
||
| 58 | } |
||
| 59 | |||
| 60 | public static String retornarApenasNumeros(String valor) { |
||
| 61 | return VerificadorUtil.naoEstaNulo(valor)? valor.replaceAll("[^0-9]", "") : ""; |
||
| 62 | } |
||
| 63 | |||
| 64 | public static String formatarValor(Double valor) { |
||
| 65 | DecimalFormat doisDigitos = new DecimalFormat("###,##0.00"); |
||
| 66 | return doisDigitos.format(valor); |
||
| 67 | } |
||
| 68 | |||
| 69 | public static String formatarPercentual(Double valor) { |
||
| 70 | DecimalFormat percentual = new DecimalFormat("###,##0%"); |
||
| 71 | return percentual.format(valor); |
||
| 72 | } |
||
| 73 | |||
| 74 | public static String lpadTo(String input, int width, char ch) { |
||
| 75 | String strPad = ""; |
||
| 76 | StringBuffer sb = new StringBuffer(input.trim()); |
||
| 77 | while (sb.length() < width) sb.insert(0,ch); |
||
| 78 | strPad = sb.toString(); |
||
| 79 | if (strPad.length() > width) { |
||
| 80 | strPad = strPad.substring(0,width); |
||
| 81 | } |
||
| 82 | return strPad; |
||
| 83 | } |
||
| 84 | |||
| 85 | public static String formatarValorComDoisDigitos(String valor) throws Exception { |
||
| 86 | try { |
||
| 87 | return formatarValorComDigitos(valor, 2); |
||
| 88 | } catch (Exception e) { |
||
| 89 | e.printStackTrace(); |
||
| 90 | throw new Exception(e.getMessage()); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | public static String formatarValorComDezDigitos(String valor) throws Exception { |
||
| 95 | try { |
||
| 96 | return formatarValorComDigitos(valor, 10); |
||
| 97 | } catch (Exception e) { |
||
| 98 | e.printStackTrace(); |
||
| 99 | throw new Exception(e.getMessage()); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | public static String formatarValorComDigitos(String valor, Integer digitos) throws Exception { |
||
| 104 | try { |
||
| 105 | DecimalFormat df = new DecimalFormat("0.##"); |
||
| 106 | if (valor.contains(".") && valor.contains(",")) { |
||
| 107 | valor = valor.replace(".", ""); |
||
| 108 | } |
||
| 109 | valor = valor.replace(",", "."); |
||
| 110 | valor = df.format(new Double(valor)).replace(",", "."); |
||
| 111 | if (!valor.contains(".")) { |
||
| 112 | valor = valor + ".00"; |
||
| 113 | } |
||
| 114 | String valorFormatar = valor.substring(valor.indexOf(".") + 1); |
||
| 115 | return valor.substring(0, valor.indexOf(".") + 1) + StringUtils.rightPad(valorFormatar, digitos, "0"); |
||
| 116 | } catch (Exception e) { |
||
| 117 | e.printStackTrace(); |
||
| 118 | throw new Exception(e.getMessage()); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | public static int modulo11(String chave) { |
||
| 123 | int total = 0; |
||
| 124 | int peso = 2; |
||
| 125 | for (int i = 0; i < chave.length(); i++) { |
||
| 126 | total += (chave.charAt((chave.length()-1) - i) - '0') * peso; |
||
| 127 | peso ++; |
||
| 128 | if (peso == 10) { |
||
| 129 | peso = 2; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | int resto = total % 11; |
||
| 133 | return (resto == 0 || resto == 1) ? 0 : (11 - resto); |
||
| 134 | } |
||
| 135 | |||
| 136 | } |