我有一个像café 这样的字符串,我需要将它翻译成cafe。
我试过 (string-normalize-nfd "café")
但它返回 cafe 一个带重音的引号,和 `(string-normalize-nfd alguém) 返回重音为 m 的 alguem。
如何将重音字符串转换为非重音字符串?
最佳答案
我想不出一个内置过程可以满足您的需求,但是编写您自己的实现很容易:
; maps accented chars to unaccented chars
(define translate
'#hash((#\á . #\a)
(#\é . #\e)
(#\í . #\i)
(#\ó . #\o)
(#\ú . #\u)))
(define (remove-accents str)
(apply string ; convert char list back into string
; for each char: replace it with non-accented
; version, if not present leave it unmodified
(map (λ (c) (hash-ref translate c (const c)))
(string->list str)))) ; convert string to char list
请确保根据需要添加更多映射,例如包含大写字符等。它按预期工作:
(remove-accents "café")
=> "cafe"
关于string - 如何从 Racket 中的字符串中删除重音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51633087/