Capital letter in Ruby with UTF-8 strings with exceptions

I would like to use every word of a UTF-8 string. However, I need the function to ignore some special characters at the beginning of words, such as "(-."). The function will be used for the headings of songs, which may look like this:

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

... outputs:

Marco, Gabriel Bonnie, Simple Jack - Recall (Original Mix)

It should also be able to use the letters UTF-8, for example, "å"> "Å". "é"> "É".

+3
source share
3 answers
"åbc".mb_chars.capitalize
#=> "Åbc" 
"ébc".mb_chars.capitalize.to_s
#=> "Ébc"

UPD

And ignore a single character of the word:

string = "-åbc"
str = string.match(/^(\W*)(.*)/)
str[1] + str[2].mb_chars.capitalize.to_s
#=> "-Åbc" 
+3
source

-, Unicode:: capize unicode ?

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 
=> "-åäöéèí"
+8

.

initializers/constants.rb

letters = ("a".."z").collect
numbers = ("1".."9").collect
symbols = %w[! @ # $ % ^ & * ( ) _ - + = | \] { } : ; ' " ? / > . < , ]
FILTER = letters + numbers + symbols

And then he just checked to see if he was in my filter:

if !FILTER.include?(c)
    #no
else
    #yes
end

You can also check the Unicode value, but you need to know the range or specific values. I did it with Chinese characters, so I got my values. I will send the code simply to give you an idea:

def check(char)
   char = char.unpack('U*').first
   if char >= 0x4E00 && char <= 0x9FFF
     return true
   end
   if char >= 0x3400 && char <= 0x4DBF
     return true
   end
   if char >= 0x20000 && char <= 0x2A6DF
     return true
   end
   if char >= 0x2A700 && char <= 0x2B73F
     return true
   end
   return false
 end

Of course you need to know the specific meanings.

+1
source

All Articles