Home > Back-end >  How to remove spanish accents but keep the "ñ"
How to remove spanish accents but keep the "ñ"

Time:04-14

I'm triying to remove accents in a string, but keeping the letter ñ.

"DIVISIÓN DE MONTAÑA" |> stringi::stri_trans_general("Latin-ASCII") does the job of removing all accents and the letter ñ, resulting in DIVISION DE MONTANA.

Is there other transformation available in stringi, that keeps the letter ñ?

CodePudding user response:

x <- "DIVISIÓN DE MONTAÑA"

paste(sapply(strsplit(x, "")[[1]], function(x) ifelse(x %in% c("Ñ", "ñ"), x, stringi::stri_trans_general(x, "Latin-ASCII"))), collapse = "")

# [1] "DIVISION DE MONTAÑA"

CodePudding user response:

One way to circumvent it is to replace the ñ with a character you don't need before the transliteration and replace them back afterwards like so:

"DIVISIÓN DE MONTAÑñA" |> 
  stringi::stri_replace_all_regex(pattern = c("ñ", "Ñ"), replacement = c("¬", "¤"), vectorize=F) |> 
  stringi::stri_trans_general("Latin-ASCII") |> 
  stringi::stri_replace_all_regex(pattern = c("¬", "¤"), replacement = c("ñ", "Ñ"), vectorize=F)

It's not the prettiest solution tho. And you have to make sure the symbols are not being transliterated themselves. The merit however is that the process is very quick and it will save time compared to a character by character strsplit

  • Related