Reputation: 5210
How can i elegantly delete all the characters from the end of the string to make it match the exact length.
For example i have a string 1234567...
. I need a string with length 6, so i need to delete 7...
. Note 7...
may contain any amount of symbols.
Upvotes: 0
Views: 99
Reputation: 83680
string = "abcdefghigkl"
# get first 7 chars without affecting on original string
new_string = string[0,6]
# force remove from 7th char
string[6..-1] = ""
string
#=> "abcdef"
Upvotes: 2
Reputation: 66837
You can index a string with a range:
s = "12345678"
s[0...6] # => "123456"
Upvotes: 3