# Get remaing characters in a string

**URL:** <https://www.ruby-forum.com/t/get-remaing-characters-in-a-string/81300>\
**Category:** Ruby\
**Created:** [January 7, 2007, 1:22am UTC](https://www.ruby-forum.com/t/get-remaing-characters-in-a-string/81300 "2007-01-07T01:22:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Claus\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@Claus\_G](https://www.ruby-forum.com/u/Claus_G)\
**Post date:** [January 7, 2007, 1:22am UTC](https://www.ruby-forum.com/t/get-remaing-characters-in-a-string/81300/1 "2007-01-07T01:22:37Z")

</div>

Hi.

I need to get the last three characters in a string. I couldn’t find a  
function in the String class so I came up with this:

class Remainder

attr\_writer :n

def initialize(t = “”, n = 0)  
@t = t.to\_s  
@n = n.to\_i  
end

def get  
get\_remaining\_letters  
end

private  
def get\_remaining\_letters  
s = @t.size  
if s \< @n  
r = 0  
else  
r = s - @n  
end

ft = @t.slice(r, @n)  
end

end

t = Remainder.new(“Guttesen”, 5)  
puts t.get  
tesen  
t.n = 4  
puts t.get  
esen

Did I reinvent the wheel or does it make sense to add it to String?

regards  
Claus

---

<div class="post-metadata">

**Author:** ![Claus\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@Claus\_G](https://www.ruby-forum.com/u/Claus_G)\
**Post date:** [January 7, 2007, 1:26am UTC](https://www.ruby-forum.com/t/get-remaing-characters-in-a-string/81300/2 "2007-01-07T01:26:37Z")

</div>

On 2007-01-07 01:21:54 +0100, “Claus G.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> Hi.
> 
> I need to get the last three characters in a string. I couldn’t find a  
> function in the String class so I came up with this:
> 
> Did I reinvent the wheel or does it make sense to add it to String?

I’d just do str.slice!(-n … -1) instead:

str = “Hello World”  
str.slice!(-3 … -1) # =\> “rld”  
str # =\> “Hello Wo”  
str.slice!(-1 … -1) # =\> “o”  
str # =\> “Hello W”

---

<div class="post-metadata">

**Author:** ![Claus\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@Claus\_G](https://www.ruby-forum.com/u/Claus_G)\
**Post date:** [January 7, 2007, 1:27am UTC](https://www.ruby-forum.com/t/get-remaing-characters-in-a-string/81300/3 "2007-01-07T01:27:37Z")

</div>

You can use String#[] with a Range instead:

> > “asdfhsdfabc”[-3…-1]  
> > =\> “abc”

or you can specify the length:

> > “asdfhsdfabc”[-3, 3]  
> > =\> “abc”

Negative numbers count from the end of the string. So basically the  
strings length plus your negative number is the position from the start  
of the string.

Dan

---

<div class="post-metadata">

**Author:** ![Claus\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@Claus\_G](https://www.ruby-forum.com/u/Claus_G)\
**Post date:** [January 7, 2007, 1:37am UTC](https://www.ruby-forum.com/t/get-remaing-characters-in-a-string/81300/4 "2007-01-07T01:37:38Z")

</div>

> > I need to get the last three characters in a string. I couldn’t find a  
> > function in the String class so I came up with this:
> 
> You can index strings from the right with negative numbers:
> 
> “abcdef”[-3…-1] =\> “def”
> 
> I think that might be all you need.

I see. Thank you very much for all the replies.

regards  
Claus

---

<div class="post-metadata">

**Author:** ![Claus\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@Claus\_G](https://www.ruby-forum.com/u/Claus_G)\
**Post date:** [January 7, 2007, 9:21am UTC](https://www.ruby-forum.com/t/get-remaing-characters-in-a-string/81300/5 "2007-01-07T09:21:07Z")

</div>

Claus G. wrote:

> @t = t.to\_s  
> if s \< @n
> 
> Claus  
> If the length of the string is less than 3,  
> the entire string will be returned.

s = “ab”  
s[/.{0,3}$/]  
s[-3,3] || s

---

<div class="post-metadata">

**Author:** ![Claus\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@Claus\_G](https://www.ruby-forum.com/u/Claus_G)\
**Post date:** [January 7, 2007, 1:29am UTC](https://www.ruby-forum.com/t/get-remaing-characters-in-a-string/81300/6 "2007-01-07T01:29:37Z")

</div>

On Sun, 7 Jan 2007, Claus G. wrote:

> @t = t.to\_s  
> if s \< @n
> 
> t = Remainder.new(“Guttesen”, 5)  
> puts t.get  
> tesen  
> t.n = 4  
> puts t.get  
> esen
> 
> Did I reinvent the wheel or does it make sense to add it to String?

You can index strings from the right with negative numbers:

“abcdef”[-3…-1] =\> “def”

I think that might be all you need.

David
