Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace special characters Ruby

I want to replace all these characters: 'àáäâãèéëẽêìíïîĩòóöôõùúüûũñç' to 'aaaaaeeeeeiiiiiooooouuuuunc'.

Is there a effective way to do this in Ruby? I was thinking about loop each character, but it's not effective.

Thanks.

like image 281
developer033 Avatar asked Dec 14 '22 08:12

developer033


1 Answers

I would use tr which is faster than a Regexp when replacing single characters:

string = 'hàllò wörld'
string.tr('àáäâãèéëẽêìíïîĩòóöôõùúüûũñç', 'aaaaaeeeeeiiiiiooooouuuuunc')
#=> '"hallo world"'
like image 67
spickermann Avatar answered Jan 05 '23 01:01

spickermann