Created
October 21, 2012 21:30
-
-
Save rodolfofadino/3928580 to your computer and use it in GitHub Desktop.
Util
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Util | |
{ | |
public static string RemoveAcentos(string palavra) | |
{ | |
string palavraSemAcento = null; | |
string caracterComAcento = "áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ"; | |
string caracterSemAcento = "aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC"; | |
for (int i = 0; i < palavra.Length; i++) | |
{ | |
if (caracterComAcento.IndexOf(Convert.ToChar(palavra.Substring(i, 1))) >= 0) | |
{ | |
int car = caracterComAcento.IndexOf(Convert.ToChar(palavra.Substring(i, 1))); | |
palavraSemAcento += caracterSemAcento.Substring(car, 1); | |
} | |
else | |
{ | |
palavraSemAcento += palavra.Substring(i, 1); | |
} | |
} | |
return palavraSemAcento; | |
} | |
public static string RemoveCaracteresEspeciais(string palavra) | |
{ | |
if (palavra == null) | |
return string.Empty; | |
string palavraLimpa = palavra | |
.Replace("\"", "") | |
.Replace("&", "") | |
.Replace("<", "") | |
.Replace(">", "") | |
.Replace("?", "") | |
.Replace("-", "") | |
.Replace("#", "") | |
.Replace(";", "") | |
.Replace("(", "") | |
.Replace(")", "") | |
.Replace("{", "") | |
.Replace("}", "") | |
.Replace("!", "") | |
.Replace("%", "") | |
.Replace(":", "") | |
.Replace(".", "") | |
.Replace("ª", "") | |
.Replace("º", "") | |
.Replace("=", "") | |
.Replace("/", "") | |
.Replace("\\", "") | |
.Replace("*", "") | |
.Replace("'", "") | |
.Replace("@", "") | |
.Replace("$", "") | |
.Replace("¨", "") | |
.Replace("+", "") | |
.Replace("~", "") | |
.Replace("]", "") | |
.Replace(",", "") | |
.Replace("nbsp", "") | |
.Replace("[", ""); | |
return palavraLimpa; | |
} | |
public static T CastAnonymous<T>(object obj, T example) | |
{ | |
return (T)obj; | |
} | |
public static List<T> GetAnonymousList<T>(T objAnonimo) | |
{ | |
List<T> newList = new List<T>(); | |
return newList; | |
} | |
public static string GetExtensaoArquivo(string nomeArquivo) | |
{ | |
string extensao = string.Empty; | |
int iExtensao = nomeArquivo.LastIndexOf("."); | |
if (iExtensao > -1) | |
{ | |
extensao = nomeArquivo.Substring(iExtensao, nomeArquivo.Length - iExtensao); | |
} | |
return extensao; | |
} | |
public static string TransformaTextoEmURL(string texto) | |
{ | |
string url = string.Empty; | |
string textoSemAcento = Util.RemoveAcentos(texto); | |
textoSemAcento = Util.RemoveCaracteresEspeciais(textoSemAcento); | |
string[] palavras = textoSemAcento.Trim().Split(new char[] { ' ' }); ; | |
for (int i = 0; i < palavras.Length; i++) | |
{ | |
url += palavras[i]; | |
if (i < palavras.Length - 1) | |
{ | |
url += "-"; | |
} | |
} | |
return url.ToLower(); | |
} | |
public static int GetCodigoURL(string urlAmigavel) | |
{ | |
int iBarra = urlAmigavel.LastIndexOf("/"); | |
string urlPura = urlAmigavel.Substring(iBarra + 1, urlAmigavel.Length - (iBarra + 1)); | |
int iMais = urlPura.IndexOf("-"); | |
int codigoURL = 0; | |
if (iMais > -1) | |
{ | |
int.TryParse(urlPura.Substring(0, iMais), out codigoURL); | |
} | |
return codigoURL; | |
} | |
public static string GetTagURL(string urlAmigavel) | |
{ | |
int Inicio = urlAmigavel.IndexOf("/tag/"); | |
Inicio += 4; | |
string urlPura = urlAmigavel.Substring(Inicio + 1).Replace(".htm", "").ToString(); | |
return urlPura; | |
} | |
public static String ConverteLista(List<Int32> lista, String delimitador) | |
{ | |
String listaConcatenada = ""; | |
foreach (Int32 item in lista) | |
{ | |
listaConcatenada += item.ToString() + "" + delimitador; | |
} | |
if (lista.Count > 0) | |
return listaConcatenada.Remove(listaConcatenada.Length - 1); | |
else | |
return listaConcatenada; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment