比较 ruby​​ 中的两个字符串 [英] comparing two strings in ruby

查看:48
本文介绍了比较 ruby​​ 中的两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习 ruby​​,这可能很容易解决.如何在 Ruby 中比较两个字符串?

I've just started to learn ruby and this is probably very easy to solve. How do I compare two strings in Ruby?

我尝试了以下方法:

puts var1 == var2 //false, should be true (I think)
puts var1.eql?(var2) //false, should be true (I think)

当我尝试将它们回显到控制台以便我可以直观地比较值时,我这样做:

When I try to echo them to console so I can compare values visually, I do this :

puts var1 //prints "test content" without quotes
puts var2 //prints ["test content"] with quotes and braces

归根结底,这些不同类型的字符串如何比较这两者?

Ultimately are these different types of strings of how do I compare these two?

推荐答案

从您打印的内容来看,var2 似乎是一个包含一个字符串的数组.或者实际上,它似乎保存在包含一个字符串的数组上运行 .inspect 的结果.展示您如何初始化它们会很有帮助.

From what you printed, it seems var2 is an array containing one string. Or actually, it appears to hold the result of running .inspect on an array containing one string. It would be helpful to show how you are initializing them.

irb(main):005:0* v1 = "test"
=> "test"
irb(main):006:0> v2 = ["test"]
=> ["test"]
irb(main):007:0> v3 = v2.inspect
=> "[\"test\"]"
irb(main):008:0> puts v1,v2,v3
test
test
["test"]

这篇关于比较 ruby​​ 中的两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆