class Bizside::StringUtils

Constants

Characters

Public Class Methods

compress_doble_byte_space(string) click to toggle source

複数個の全角スペースを1個に圧縮する

# File lib/bizside/string_utils.rb, line 127
def self.compress_doble_byte_space(string)
  return nil unless string
  string.gsub(/ +/, ' ')
end
create_random_alpha_string(length, case_sensitive = false) click to toggle source
# File lib/bizside/string_utils.rb, line 7
def self.create_random_alpha_string length, case_sensitive = false
  chars = ('a'..'z').to_a
  chars += ('A'..'Z').to_a if case_sensitive
  chars.sample(length).join
end
create_random_string(number) click to toggle source
# File lib/bizside/string_utils.rb, line 13
def self.create_random_string(number)
  (Array.new(number.to_i) do
    Characters[rand(Characters.size)]
  end
  ).join
end
current_time_string(now = nil) click to toggle source
# File lib/bizside/string_utils.rb, line 60
def self.current_time_string(now = nil)
  now = Time.now unless now
  now.instance_eval { '%s%03d' % [strftime('%Y%m%d%H%M%S'), (usec / 1000.0).round] }
end
equal(s1, s2) click to toggle source
# File lib/bizside/string_utils.rb, line 52
def self.equal(s1, s2)
  if is_empty(s1) and is_empty(s2)
    true
  else
    s1 == s2
  end
end
exclude_query_string(string) click to toggle source

URLからクエリストリングを除外して文字列を返す。 www.example.com?foo=bar #=> www.example.com

# File lib/bizside/string_utils.rb, line 121
def self.exclude_query_string(string)
  return string unless string.include?("?")
  string[/^.*(?=\?)/]
end
is_empty(s) click to toggle source
# File lib/bizside/string_utils.rb, line 48
def self.is_empty(s)
  s.nil? or s.empty?
end
is_login_id(value) click to toggle source
# File lib/bizside/string_utils.rb, line 73
def self.is_login_id(value)
  return nil if value =~ /\.{2,}/
  value =~ /^[0-9a-z_\.\-]{1,50}$/u
end
is_not_login_id(value) click to toggle source
# File lib/bizside/string_utils.rb, line 78
def self.is_not_login_id(value)
  ! is_login_id(value)
end
is_not_user_password(value,minimum,maximum) click to toggle source
# File lib/bizside/string_utils.rb, line 69
def self.is_not_user_password(value,minimum,maximum)
   ! is_user_password(value,minimum,maximum)
end
is_user_password(value,minimum,maximum) click to toggle source
# File lib/bizside/string_utils.rb, line 65
def self.is_user_password(value,minimum,maximum)
  value =~ /^[!-~]{#{minimum},#{maximum}}$/u
end
rand_string(length = 8) click to toggle source

指定した長さのランダム英数字文字列を返す。a-zA-Z0-9。62種類。

# File lib/bizside/string_utils.rb, line 100
def self.rand_string(length = 8)
  ret = []
  length.times do |i|
    ret[i] = Characters[rand(Characters.size)]
  end
  ret = ret.join

  return ret if ret =~ /^(?=.*?[a-z])(?=.*?\d).*+$/iu

  # 英字と数字が最低1文字を含むランダム英数字文字列を返すようにする
  alphabets = ('a'..'z').to_a + ('A'..'Z').to_a
  numerics = (0..9).to_a
  ranges = (0...length).to_a
  ret[ranges.slice!(rand(ranges.size))] = alphabets[rand(alphabets.size)]
  ret[ranges.slice!(rand(ranges.size))] = numerics[rand(numerics.size)].to_s

  ret
end
remove_html_tag(str) click to toggle source

HTMLタグを除去した文書を返す

# File lib/bizside/string_utils.rb, line 83
def self.remove_html_tag(str)
  return nil unless str
  str=str.dup
  str.sub!(/<[^<>]*>/,"") while /<[^<>]*>/ =~ str
  str
end
remove_rich_html_tag(str) click to toggle source

リッチテキストの改行を保ったまま、HTMLタグを除去した文書を返す

# File lib/bizside/string_utils.rb, line 91
def self.remove_rich_html_tag(str)
  return nil unless str

  ret = str.gsub(/<div[^>]*>/, "\n")
  ret = remove_html_tag(ret)
  ret
end
replace_char_like_hyphen_to(string, to: "\u30FC") click to toggle source

文字列中に含まれる全角ハイフンっぽい文字を、指定した文字に一括置換する

# File lib/bizside/string_utils.rb, line 133
def self.replace_char_like_hyphen_to(string, to: "\u30FC")
  # U+002D Hyphen-Minus
  # U+00AD Soft Hyphen
  # U+02D7 Modifier Letter Minus Sign
  # U+2010 Hyphen
  # U+2011 Non-Breaking Hyphen
  # U+2012 Figure Dash
  # U+2013 En Dash
  # U+2014 Em Dash
  # U+2015 Horizontal Bar
  # U+2043 Hyphen Bullet
  # U+2212 Minus Sign
  # U+2796 Heavy Minus Sign
  # U+30FC Katakana-Hiragana Prolonged Sound Mark
  # U+FE58 Small Em Dash
  # U+FE63 Small Hyphen-Minus
  # U+FF0D Fullwidth Hyphen-Minus
  # U+FF70 Halfwidth Katakana-Hiragana Prolonged Sound Mark
  string.gsub(
    /[\u002D\u00AD\u02D7\u2010\u2011\u2012\u2013\u2014\u2015\u2043\u2212\u2796\u30FC\uFE58\uFE63\uFF0D\uFF70]/,
    to
  )
end
sjis_to_utf8(s) click to toggle source
# File lib/bizside/string_utils.rb, line 40
def self.sjis_to_utf8(s)
  NKF.nkf('-Swx', s)
end
to_han(s) click to toggle source
# File lib/bizside/string_utils.rb, line 35
def self.to_han(s)
  return s if is_empty(s)
  NKF::nkf('-Wwxm0Z0', s)
end
to_hiragana(s) click to toggle source
# File lib/bizside/string_utils.rb, line 20
def self.to_hiragana(s)
  return s if is_empty(s)
  NKF::nkf('-Ww --hiragana', s)
end
to_katakana(s) click to toggle source
# File lib/bizside/string_utils.rb, line 25
def self.to_katakana(s)
  return s if is_empty(s)
  NKF::nkf('-Ww --katakana', s)
end
to_zen(s) click to toggle source
# File lib/bizside/string_utils.rb, line 30
def self.to_zen(s)
  return s if is_empty(s)
  NKF::nkf('-WwXm0', s)
end
utf8_to_sjis(s) click to toggle source
# File lib/bizside/string_utils.rb, line 44
def self.utf8_to_sjis(s)
  NKF.nkf('-Wsx', s)
end