quinta-feira, 4 de junho de 2009

Classe utilitaria Strings

package C_utils;

public abstract class Strings {

public static void main(String[] args) {
}

/**
*
* @param substituir: array de string a serem substituidas no texto
* @param por: String que substituira os trechos
* @param texto texto que contem as strings a serem substituidas
* @return retorna uma string com as substituições.
*/
public static String substituirTrechos(String[] substituir, String por, String texto) {
if (texto == null) {
return "";
}

for (String temp : substituir) {
texto = texto.replace(temp, por);
}
return texto;
}

/**
*
* @param tamanho da string para os espaços em branco
* @return retorna uma string com espaços em branco
*/
public static String espacosEmBranco(int tamanho) {
String temp = "";

for (int i = 1; i <= tamanho; i++) { temp += " "; } return temp; } /** * se o tamanho especificado for menor que o tamanho ja existente da string ele nao faz nada * @param texto string que devera ser modificada * @param tamanho tamanho que deve ficar a string * @param charPreencher caracter que preenchera os espaços vazios * @param ondePreencher se é "ATRAZ" ou na "FRENTE" da string * @return retorna a string modificada */ public static String defineTamanhoString(String texto, int tamanho, char charPreencher, String ondePreencher) { int sobra = tamanho - texto.length(); if (sobra < texto =" texto.substring(0,"> 0) {
String temp = "";
int i = 1;
while (i <= sobra) {
temp += charPreencher;
i++;
}
if (ondePreencher.equals("FRENTE")) {
texto = temp + texto;
} else {
texto = texto + temp;
}
texto = texto.substring(0, tamanho);
}
return texto;
}
}

Nenhum comentário:

Postar um comentário