Ruby:如何判断字符是大写还是小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12713251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:26:41  来源:igfitidea点击:

Ruby: how to tell if character is upper/lowercase

ruby

提问by George Armhold

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 Stringclass.

我很惭愧地问这个,因为这似乎应该是显而易见的,但是如何判断字符串中的给定字符在 Ruby 中是大写还是小写?我在String课堂上看不到明显的罐头解决方案。

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

我采用了以下不考虑非 ASCII 代码的方法:

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?

有什么更惯用的吗?

回答by fells77

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

使用正则表达式模式:[AZ] 或:

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

回答by Mischa

I don't think there is something more idiomatic. The only thing you could do -- instead of passing in the string as an argument -- is monkey patch the Stringclass:

我认为没有什么比这更惯用的了。你唯一可以做的——而不是将字符串作为参数传递——是猴子补丁String类:

class String
  def is_upper?
    self == self.upcase
  end

  def is_lower?
    self == self.downcase
  end
end

"a".is_upper? #=> false
"A".is_upper? #=> true

Using the method in the answersuggested by the commenter above and monkey patching String, you could do this:

使用上面评论者和猴子补丁建议的答案中的方法String,您可以执行以下操作:

class String
  def is_upper?
    !!self.match(/\p{Upper}/)
  end

  def is_lower?
    !!self.match(/\p{Lower}/)
    # or: !self.is_upper?
  end
end

回答by Neil Stockbridge

What does it mean for a string to be lower case? Does it mean that the string only contains lower case characters or that it doesn't contain any upper case characters? In my case I want:

字符串小写是什么意思?这是否意味着该字符串仅包含小写字符或不包含任何大写字符?就我而言,我想要:

"a2".is_lower? #=> true

..which leads me to:

..这导致我:

class String

  def is_upper?
    not self.match /[[:lower:]]/
  end

  def is_lower?
    not self.match /[[:upper:]]/
  end

end

Note that /\p{Lower}/might be better but is unsupported on Ruby 1.8.

请注意,这/\p{Lower}/可能更好,但在 Ruby 1.8 上不受支持。

回答by crownusa

The easiest way to verify if a character is uppercase or lowercase:

验证字符是大写还是小写的最简单方法:

#Can be any character
char = 'a'
if char === char.capitalize then
  return 'Character is uppercase.'
else
  return 'Character is lowercase.'
end

This very simplistic if loopcan determine the 'case' of a letter, by checking if it is equal to it's uppercase form. If it's already uppercase, it'll obviously be true.

这种非常简单的方法if loop可以通过检查字母是否等于它的大写形式来确定字母的“大小写”。如果它已经是大写的,那显然是true.

回答by Andrew Smith

Matching a conversion doesn't emulate the functionality of the libc isupper()and islower()functions in that both should return falsefor non alpha.

匹配转换不会模拟 libcisupper()islower()函数的功能,因为两者都应该返回false非 alpha。

Ranges seem the easiest way to do this for single characters.

范围似乎是对单个字符执行此操作的最简单方法。

class String
  def islower?
    return false if self.size > 1
    ('a'..'z').include? self
  end

  def isupper?
    return false if self.size > 1
    ('A'..'Z').include? self
  end
end

回答by Andy Stuart

Just use an if statement, e.g. if c == c.upcaseor if c == c.downcase.

只需使用 if 语句,例如if c == c.upcaseor if c == c.downcase