Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalize first letter in Ruby with UTF-8 strings with exceptions

Tags:

regex

ruby

I would like to capitalize each word of a UTF-8 string. However, I need the function to ignore some special characters in the beginning of words, like "(-.,". The function will be used to capitalize song titles which can look like this:

marko, gabriel boni, simple jack - recall (original mix)

...would output:

Marko, Gabriel Boni, Simple Jack - Recall (Original Mix)

It should also be able to capitalize UTF-8 chars like "å" > "Å". "é" > "É".

like image 962
Johan Avatar asked Dec 13 '22 12:12

Johan


2 Answers

Is there something why Unicode::capitalize method from unicode library does not suit your needs ?

irb(main):013:0> require 'unicode'
=> true
irb(main):014:0> begin Unicode::capitalize 'åäöéèí' rescue $stderr.print "unicode error\n" end
=> "Åäöéèí"
irb(main):015:0> begin Unicode::capitalize '-åäöéèí' rescue $stderr.print "unicode error\n" end 
=> "-åäöéèí"
like image 175
David Unric Avatar answered Jan 04 '23 23:01

David Unric


"åbc".mb_chars.capitalize
#=> "Åbc" 
"ébc".mb_chars.capitalize.to_s
#=> "Ébc"

UPD

And to ignore none word chars:

string = "-åbc"
str = string.match(/^(\W*)(.*)/)
str[1] + str[2].mb_chars.capitalize.to_s
#=> "-Åbc" 
like image 40
fl00r Avatar answered Jan 04 '23 23:01

fl00r