Eliminar todas las ocurrencias de char de la cadena
Puedo usar esto:
String str = "TextX Xto modifyX";
str = str.replace('X','');//that does not work because there is no such character ''
¿Hay alguna manera de eliminar todas las ocurrencias de carácter X
de una cadena en Java?
He intentado esto y no es lo que quiero: str.replace('X',' '); //replace with space
9 answers
Intente usar la sobrecarga que toma CharSequence
argumentos (por ejemplo, String
) en lugar de char
:
str = str.replace("X", "");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-27 17:12:25
Usando
public String replaceAll(String regex, String replacement)
Funcionará.
El uso sería str.replace("X", "");
.
Ejecutando
"Xlakjsdf Xxx".replaceAll("X", "");
Devuelve:
lakjsdf xx
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-28 19:18:07
Si quieres hacer algo con cadenas Java, Commons Lang StringUtils es un gran lugar para buscar.
StringUtils.remove("TextX Xto modifyX", 'X');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-18 12:11:14
String test = "09-09-2012";
String arr [] = test.split("-");
String ans = "";
for(String t : arr)
ans+=t;
Este es el ejemplo para donde he eliminado el carácter - de la cadena.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-27 15:25:50
Me gusta usar expresiones regulares en esta ocasión:
str = str.replace(/X/g, '');
Donde g significa global por lo que pasará por toda la cadena y reemplazará todas las X con "; si desea reemplazar X y x, simplemente diga:
str = str.replace(/X|x/g, '');
(ver mi violín aquí: violín)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-16 15:40:11
Hola Pruebe este código a continuación
public class RemoveCharacter {
public static void main(String[] args){
String str = "MXy nameX iXs farXazX";
char x = 'X';
System.out.println(removeChr(str,x));
}
public static String removeChr(String str, char x){
StringBuilder strBuilder = new StringBuilder();
char[] rmString = str.toCharArray();
for(int i=0; i<rmString.length; i++){
if(rmString[i] == x){
} else {
strBuilder.append(rmString[i]);
}
}
return strBuilder.toString();
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-21 07:17:22
Use replaceAll en lugar de replace
str = str.replaceAll("X,"");
Esto debería darte la respuesta deseada.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-23 21:44:04
package com.acn.demo.action;
public class RemoveCharFromString {
static String input = "";
public static void main(String[] args) {
input = "abadbbeb34erterb";
char token = 'b';
removeChar(token);
}
private static void removeChar(char token) {
// TODO Auto-generated method stub
System.out.println(input);
for (int i=0;i<input.length();i++) {
if (input.charAt(i) == token) {
input = input.replace(input.charAt(i), ' ');
System.out.println("MATCH FOUND");
}
input = input.replaceAll(" ", "");
System.out.println(input);
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-30 08:29:55
Puede usar str = str.replace("X", "");
como se mencionó anteriormente y estará bien. Para su información ''
no es un carácter vacío (o válido), pero '\0'
lo es.
Así que podrías usar str = str.replace('X', '\0');
en su lugar.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-04 08:47:25