
Cambiar caracteres en cadenas
Publicado por Nicolas (2 intervenciones) el 29/06/2016 18:49:05
Amigos les pido ayuda a mi problema!!
le he dado muchas vueltas y aun no puedo resolver.
les cuento.
tengo una cadena de string, donde hay caracteres extraños, los que tengo que reemplazarlos e insertarlos nuevamente en la cadena hasta cambiarlos todos....
la cuestion es que solo hace el primer caracter y sale...
porfa si me pueden ayudar!
texto = 'U*KN951850*SEPUÁÀÄDA/´ANUEL'
resultado= U*KN951850*SEPUAÀÄDA/´ANUEL'
mis codigos:
le he dado muchas vueltas y aun no puedo resolver.
les cuento.
tengo una cadena de string, donde hay caracteres extraños, los que tengo que reemplazarlos e insertarlos nuevamente en la cadena hasta cambiarlos todos....
la cuestion es que solo hace el primer caracter y sale...
porfa si me pueden ayudar!
texto = 'U*KN951850*SEPUÁÀÄDA/´ANUEL'
resultado= U*KN951850*SEPUAÀÄDA/´ANUEL'
mis codigos:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public String cambiarCaracter(String string, Hashtable<String,String> ht){
String ret="";
String texto="";
try{
String cadena = "ñ_´'`áàäéèëíìïóòöúùñÂÂÁÀÄÉÈËÍÌÏÓÒÖÚÙÜÑçÇ!¡°";
texto = string.trim();
for(int i = 0; i < texto.length(); ++i) {
char caracter = texto.charAt(i);
if(!Character.isSpaceChar(caracter)){
if(!Character.isWhitespace(caracter)){
if(cadena.contains(String.valueOf(caracter))){
ret=String.valueOf(ht.get(String.valueOf(texto.charAt(i))).toString());
texto=texto.replace(String.valueOf(texto.charAt(i)), ret);
return texto;
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
return texto;
}
public Hashtable<String,String> cargaCaracter() {
Hashtable<String,String> ht=new Hashtable<String,String>();
try{
ht.put("ñ", "n");
ht.put("_", "-");
ht.put("´", "");
ht.put("'", "");
ht.put("`", "");
ht.put("á", "a");
ht.put("à", "a");
ht.put("ä", "a");
ht.put("é", "e");
ht.put("è", "e");
ht.put("ë", "e");
ht.put("í", "i");
ht.put("ì", "i");
ht.put("ï", "i");
ht.put("ó", "o");
ht.put("ò", "o");
ht.put("ö", "o");
ht.put("ú", "u");
ht.put("ù", "u");
ht.put("ñ", "n");
ht.put("Â", "A");
ht.put("Á", "A");
ht.put("À", "A");
ht.put("Ä", "A");
ht.put("É", "E");
ht.put("È", "E");
ht.put("Ë", "E");
ht.put("Í", "I");
ht.put("Ì", "I");
ht.put("Ï", "I");
ht.put("Ó", "O");
ht.put("Ò", "O");
ht.put("Ö", "O");
ht.put("Ú", "U");
ht.put("Ù", "U");
ht.put("Ü", "U");
ht.put("Ñ", "N");
ht.put("ç", "");
ht.put("Ç", "");
ht.put("!", "");
ht.put("¡", "");
ht.put("°", "");
}catch (Exception e){
e.printStackTrace();
}
return ht;
}
Valora esta pregunta


0