简体   繁体   中英

Ruby string ignore last character

how to ignore last character inside string in 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 ! will change the content in place.

试试这个解决方案:

my_string[0..-2]

Negative indices index from the end, -1 is the last character; 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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