ruby - 对 Ruby 长度属性感到困惑

标签 ruby string

为什么我的 foo() 函数打印出长度为 5 的字符串“YAAR”?

def foo() 
map = Hash.new
File.open('dictionary.txt').each_line{ |s|
    word = s.split(',')
    if word.any? { |b| b.include?('AA') }
        puts word.last
        puts word.last.length
    end
    }
end

一些文件.txt

265651,YAAR
265654,YAARS

输出

YAAR
5
YAARS
6

最佳答案

你在两个字符串的末尾都有一个换行符 '\n'。因此,您的拆分收到:

"265651,YAAR\n"
"265654","YAARS\n"

关于ruby - 对 Ruby 长度属性感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15125602/

相关文章:

c# - 在双向文本中,点右侧的字符串宽度

python - 不仅查找字符串中子字符串的第一个索引 - python 2.7

ruby-on-rails - 正则表达式用于规范 Discourse 论坛中的主题链接

ruby-on-rails - ruby on rails 中的设计错误

ruby-on-rails - Rails 应用程序中数据对话的最佳实践

ruby - 使用 Axlsx,是否可以为整个列定义数据类型?

java - 找到最小的子段

ruby-on-rails - 使用 RSpec 和模拟测试 after_commit

java - 使用 JavaCompiler 编译 Minecraft

c - 如何使用字符串打开二进制文件?使用 C