Skip to content
Advertisement

How can i remove different strings from one string?

I’ve been searching for a while but other answers are quiet different to what im trying to do. I’m trying to delete prepositions from a string but i wonder if there is any way to delete them without doing it one by one like i’m doing right now: (I’m trying to delete spanish prepositions like (de, del, el, la))

String content = "Martinez de Rodríguez, Rosa
Lothbrok, Ragnar
Skywalker, Anakin
Ronaldo, Cristiano
Del campo Lopez, Antonio";
String withoutPreposition = content.replace("de ", ""); 
System.out.println(withoutPreposition);

Once i do that the result is:

Martinez Rodríguez, Rosa
Lothbrok, Ragnar
Skywalker, Anakin
Ronaldo, Cristiano
Del campo Lopez, Antonio

Could i delete all prepositions at once or i need to do it one by one?

Advertisement

Answer

You can try to use regex

stringToReplaceIn.replaceAll("de |del |el |la ","");
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement