简体   繁体   English

Ruby字符串忽略最后一个字符

[英]Ruby string ignore last character

how to ignore last character inside string in ruby? 如何忽略Ruby中字符串中的最后一个字符? if i have problem like this, example : 如果我有这样的问题,例如:

abc = "123456a"

how to get result like this : 如何获得这样的结果:

abc = "123456"

i don't need last character inside the string, how to ignore it? 我不需要字符串中的最后一个字符,如何忽略它?

thanks before :) 之前感谢:)

You can try: 你可以试试:

abc.chop!  

chop will delete the last character and ! chop将删除最后一个字符和! will change the content in place. 将更改内容。

试试这个解决方案:

my_string[0..-2]

Negative indices index from the end, -1 is the last character; 负索引从末尾开始索引, -1是最后一个字符; so to get everything up to next to last character, you'd write 为了使所有内容都紧靠最后一个字符,您需要编写

abc[0..-2]

There are many ways to make it. 有很多方法可以做到这一点。

abc[0..-2]

abc[0..(abc.length-2)]

abc.delete abc[-1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM