Module: Struggle::StringExtend
- Defined in:
- lib/struggle/concerns/string_extend.rb
Instance Method Summary collapse
- #htmlClear ⇒ Object
- #isCn ⇒ Object
- #isCnOrEn ⇒ Object
- #truncate(length = 30, truncate_string = "...") ⇒ Object
Instance Method Details
#htmlClear ⇒ Object
3 4 5 |
# File 'lib/struggle/concerns/string_extend.rb', line 3 def htmlClear self.gsub(/<script.*?<\/script>/, "").gsub(/<iframe.*?<\/iframe>/, "") end |
#isCn ⇒ Object
7 8 9 |
# File 'lib/struggle/concerns/string_extend.rb', line 7 def isCn (self=~/[\u4e00-\u9fa5]/).nil? ? false : true end |
#isCnOrEn ⇒ Object
11 12 13 |
# File 'lib/struggle/concerns/string_extend.rb', line 11 def isCnOrEn (self=~/^[\u4e00-\u9fa5_a-zA-Z0-9]+$/).nil? ? false : true end |
#truncate(length = 30, truncate_string = "...") ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/struggle/concerns/string_extend.rb', line 15 def truncate(length = 30, truncate_string = "...") l=0 char_array=self.unpack("U*") char_array.each_with_index do |c, i| l = l+ (c<127 ? 0.5 : 1) if l>=length return char_array[0..i].pack("U*")+(i<char_array.length-1 ? truncate_string : "") end end return self end |