# Splitting a string

**URL:** <https://www.ruby-forum.com/t/splitting-a-string/181223>\
**Category:** Rails\
**Created:** [January 7, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/splitting-a-string/181223 "2010-01-07T16:10:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 7, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/splitting-a-string/181223/1 "2010-01-07T16:10:28Z")

</div>

Example A: “Example One”

What I need is a string method to remove " One" from that string so the  
result is just “Example”.

I’m sure this is very simple but I just require a swift response.

Thanks.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 7, 2010, 4:36pm UTC](https://www.ruby-forum.com/t/splitting-a-string/181223/2 "2010-01-07T16:36:47Z")

</div>

Pale H. wrote:

> Example A: “Example One”
> 
> What I need is a string method to remove " One" from that string so the  
> result is just “Example”.
> 
> I’m sure this is very simple but I just require a swift response.
> 
> Thanks.

a = “Example One”  
a = a.split(/ /)[0]

In general, the way to solve a problem is to try and tackle it from  
different angles.  
Do you want to …  
a) remove everything after ‘Example’?  
b) remove the last four characters of the string?  
c) remove everything after the first word?  
d) remove the last word and the last space?

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 7, 2010, 4:38pm UTC](https://www.ruby-forum.com/t/splitting-a-string/181223/3 "2010-01-07T16:38:16Z")

</div>

> a = “Example One”  
> a = a.split(/ /)[0]
> 
> In general, the way to solve a problem is to try and tackle it from  
> different angles.  
> Do you want to …  
> a) remove everything after ‘Example’?  
> b) remove the last four characters of the string?  
> c) remove everything after the first word?  
> d) remove the last word and the last space?

I want to remove everything after the first word.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 7, 2010, 6:06pm UTC](https://www.ruby-forum.com/t/splitting-a-string/181223/4 "2010-01-07T18:06:53Z")

</div>

Pale H. wrote:

> > a = “Example One”  
> > a = a.split(/ /)[0]
> > 
> > In general, the way to solve a problem is to try and tackle it from  
> > different angles.  
> > Do you want to …  
> > a) remove everything after ‘Example’?  
> > b) remove the last four characters of the string?  
> > c) remove everything after the first word?  
> > d) remove the last word and the last space?
> 
> I want to remove everything after the first word.

Well, then my solution works for you, but you should familiarize  
yourself with it.  
[http://ruby-doc.org/core/](http://ruby-doc.org/core/)

Go check out the String methods.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 7, 2010, 7:35pm UTC](https://www.ruby-forum.com/t/splitting-a-string/181223/5 "2010-01-07T19:35:28Z")

</div>

On Thu, Jan 7, 2010 at 10:10 AM, Pale H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Example A: “Example One”
> 
> What I need is a string method to remove " One" from that string so the  
> result is just “Example”.
> 
> I’m sure this is very simple but I just require a swift response.  
> How about “Example One”.split?

–wpd

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 7, 2010, 7:50pm UTC](https://www.ruby-forum.com/t/splitting-a-string/181223/6 "2010-01-07T19:50:08Z")

</div>

Quoting Pale H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Example A: “Example One”
> 
> What I need is a string method to remove " One" from that string so the  
> result is just “Example”.

irb(main):010:0\> “Example One and trailing stufff”.split(’ ', 2)[0]  
=\> “Example”

irb(main):014:0\> “Example One and trailing stuff”.slice(/\w+/)  
=\> “Example”

HTH,  
Jeffrey
