Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

Ruby: how to tell if character is upper/lowercase

I'm ashamed to ask this, because it seems like it ought to be obvious, but how does one tell whether a given character in a string is upper or lowercase in Ruby? I see no obvious canned solution in the String class.

I've resorted to the following, which does not consider non-ASCII codes:

def is_lower?(c)
  c >= 'a' && c <= 'z'
end

def is_upper?(c)
  ! is_lower(c)
end

Something else I've considered is:

def is_lower?(c)
    c != c.upcase
end

Is there something more idiomatic for this?

question from:https://stackoverflow.com/questions/12713251/ruby-how-to-tell-if-character-is-upper-lowercase

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use a regex pattern: [A-Z] or:

/[[:upper:]]/.match(c)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...