# String function to delete characters

**URL:** <https://www.ruby-forum.com/t/string-function-to-delete-characters/144714>\
**Category:** Ruby\
**Created:** [August 1, 2008, 1:16pm UTC](https://www.ruby-forum.com/t/string-function-to-delete-characters/144714 "2008-08-01T13:16:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![prasad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9de0a6/32.png) [@prasad](https://www.ruby-forum.com/u/prasad)\
**Post date:** [August 1, 2008, 1:16pm UTC](https://www.ruby-forum.com/t/string-function-to-delete-characters/144714/1 "2008-08-01T13:16:35Z")

</div>

Hi all,

```
 Do we have a string function in RUBY to delete 1st 3 characters

```

or last 3 character or 3 characters in middle from the string.

Ex:

```
 user = aaaPrasadxxxPrasadxxx

```

from above string “user” I want to delete 1st characters “aaa”  
In the same way I want to delete last 3 characters “xxx” and , need  
to delete middle 3 characters “xxx”

to delete last 3 character & 1st 3 characters, i have used code as  
below;

```
    user = user.chop.chop.chop

   user = user.reverse.chop.chop.chop.reverse

```

Other than this do we have any other string function in ruby?

could you please help in this issue.

Regards,  
Prasad.

---

<div class="post-metadata">

**Author:** ![prasad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9de0a6/32.png) [@prasad](https://www.ruby-forum.com/u/prasad)\
**Post date:** [August 1, 2008, 1:21pm UTC](https://www.ruby-forum.com/t/string-function-to-delete-characters/144714/2 "2008-08-01T13:21:52Z")

</div>

On 1 Aug 2008, at 12:15, Prasad wrote:

> In the same way I want to delete last 3 characters “xxx” and , need  
> to delete middle 3 characters “xxx”
> 
> to delete last 3 character & 1st 3 characters, i have used code as  
> below;

Depending on what you’re doing, gsub/gsub!, slice/slice! or delete may  
be what you want

gsub allows you to replace the portions of the string matching a pattern  
slice/slice! are for extracting/deleting the characters in a given range  
delete removes occurences of a particular string

Fred

---

<div class="post-metadata">

**Author:** ![prasad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9de0a6/32.png) [@prasad](https://www.ruby-forum.com/u/prasad)\
**Post date:** [August 1, 2008, 1:35pm UTC](https://www.ruby-forum.com/t/string-function-to-delete-characters/144714/3 "2008-08-01T13:35:36Z")

</div>

Prasad wrote:

> Hi all,
> 
> ```
> Do we have a string function in RUBY to delete 1st 3 characters
> 
> ```
> 
> or last 3 character or 3 characters in middle from the string.
> 
> Ex:
> 
> ```
> user = aaaPrasadxxxPrasadxxx
> 
> ```
> 
> from above string “user” I want to delete 1st characters “aaa”  
> In the same way I want to delete last 3 characters “xxx” and , need  
> to delete middle 3 characters “xxx”
> 
> to delete last 3 character & 1st 3 characters, i have used code as  
> below;
> 
> ```
> user = user.chop.chop.chop
> 
> user = user.reverse.chop.chop.chop.reverse
> 
> ```
> 
> Other than this do we have any other string function in ruby?
> 
> could you please help in this issue.

Hi Prasad,

This code only works when user=“aaaPrasadxxxPrasadxxx”

irb(main):012:0\>user = “aaaPrasadxxxPrasadxxx”  
irb(main):012:0\> user.gsub(“xxx”,“”).gsub(“aaa”,“”)  
=\> “PrasadPrasad”  
irb(main):013:0\>

In case you have more types of USer strings then just post here. We will  
try it…

Regards,  
P.Raveendran,  
RF,Chennai,

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...
