gpt4 book ai didi

ruby-on-rails - Ruby RoR 解析字符串上层

转载 作者:太空宇宙 更新时间:2023-11-03 17:29:53 25 4
gpt4 key购买 nike

我是 ruby​​ 的新手,我想解析这个字符串中的每个字符,例如 str = "Amdh#34HB!x" 并得到这样的结果:

1) 成字符串

"Upper : 3 Lower : 4 numbers : 2 special : 2"

2) 将结果存入变量

@Upper = 3
@Lower = 4
@numbers = 2
@special = 2

有什么帮助吗?

最佳答案

s = 'Amdh#34HB!x'

upper = s.scan(/[A-Z]/).count
lower = s.scan(/[a-z]/).count
numbers = s.scan(/[0-9]/).count
special = s.scan(/[^a-z0-9]/i).count

"Upper: #{upper} Lower: #{lower} Numbers: #{numbers} Special: #{special}"

#=> "Upper: 3 Lower: 4 Numbers: 2 Special: 2"

关于ruby-on-rails - Ruby RoR 解析字符串上层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43764158/

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