gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 06:24:46 25 4
gpt4 key购买 nike

我很惭愧地问这个问题,因为它看起来应该是显而易见的,但是在 Ruby 中如何判断字符串中的给定字符是大写还是小写?我在 String 类中看不到明显的固定解决方案。

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

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

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

我考虑过的其他事情是:

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

有没有更惯用的东西?

最佳答案

使用正则表达式模式:[A-Z] 或:

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

关于Ruby:如何判断字符是大写还是小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12713251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com