# How to remove ^M at the end of each line?

**URL:** <https://www.ruby-forum.com/t/how-to-remove-m-at-the-end-of-each-line/171848>\
**Category:** Rails\
**Created:** [July 10, 2009, 4:49am UTC](https://www.ruby-forum.com/t/how-to-remove-m-at-the-end-of-each-line/171848 "2009-07-10T04:49:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 10, 2009, 4:49am UTC](https://www.ruby-forum.com/t/how-to-remove-m-at-the-end-of-each-line/171848/1 "2009-07-10T04:49:57Z")

</div>

I got a string from “text\_area\_tag”. Because the browser working on  
windows while my rails server working on linux, there is ^M at the end  
of each line. What I want to do is to remove this character and separate  
the string to an array. How can I do this?

thanks.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 10, 2009, 4:59am UTC](https://www.ruby-forum.com/t/how-to-remove-m-at-the-end-of-each-line/171848/2 "2009-07-10T04:59:02Z")

</div>

On Thu, Jul 9, 2009 at 7:49 PM, Zhao Yi  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> I got a string from “text\_area\_tag”. Because the browser working on  
> windows while my rails server working on linux, there is ^M at the end  
> of each line. What I want to do is to remove this character and separate  
> the string to an array. How can I do this?
> 
> thanks.

You can try using dos2unix or others methods located here:

> **[How to remove CTRL M characters](https://aruljohn.com/blog/ctrlm/)**
>
> This is a tutorial on how to remove the Control-M character ^M at the end of line. This usually happens when you save the file in Windows.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 10, 2009, 10:22pm UTC](https://www.ruby-forum.com/t/how-to-remove-m-at-the-end-of-each-line/171848/3 "2009-07-10T22:22:41Z")

</div>

2009/7/10 Zhao Yi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I got a string from “text\_area\_tag”. Because the browser working on  
> windows while my rails server working on linux, there is ^M at the end  
> of each line. What I want to do is to remove this character and separate  
> the string to an array. How can I do this?

Can I just check that I understand correctly. You have a string with  
embedded ^M characters and you wish to turn this into an array of  
strings? If that is correct then I would suggest looking at  
String#split.

Colin

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 11, 2009, 12:42am UTC](https://www.ruby-forum.com/t/how-to-remove-m-at-the-end-of-each-line/171848/4 "2009-07-11T00:42:59Z")

</div>

On Fri, Jul 10, 2009 at 1:22 PM, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> embedded ^M characters and you wish to turn this into an array of  
> strings? If that is correct then I would suggest looking at  
> String#split.

Colin, he wants to first remove the ^M from the string. Then he wants  
to  
split  
the string.

-Conrad

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 11, 2009, 8:23am UTC](https://www.ruby-forum.com/t/how-to-remove-m-at-the-end-of-each-line/171848/5 "2009-07-11T08:23:48Z")

</div>

On Sat, Jul 11, 2009 at 12:42 AM, Conrad T.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > Can I just check that I understand correctly. You have a string with  
> > embedded ^M characters and you wish to turn this into an array of  
> > strings? If that is correct then I would suggest looking at  
> > String#split.
> 
> Colin, he wants to first remove the ^M from the string. Then he wants to  
> split  
> the string.  
> -Conrad

The ^M is a line feed shown in an editor such as vim because Linux  
only needs a carriage return (\n)

You could do the following:  
“one\r\ntwo\nthree\rfour”.split(/[\n\r]+/) which will split the string  
into “lines” at every combinations of carriage return and/or line feed

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)

[http://MyMvelope.com](http://MyMvelope.com) - The SIMPLE way to manage your savings

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 16, 2009, 7:38am UTC](https://www.ruby-forum.com/t/how-to-remove-m-at-the-end-of-each-line/171848/6 "2009-07-16T07:38:57Z")

</div>

Thanks. I think split is what I want.  
Another thing is that how I can remove the ^M from a string. I found  
that there is a method “replace” in string class. But it will replace  
the whole string, right?

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 16, 2009, 8:37am UTC](https://www.ruby-forum.com/t/how-to-remove-m-at-the-end-of-each-line/171848/7 "2009-07-16T08:37:47Z")

</div>

2009/7/16 Zhao Yi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ## Thanks. I think split is what I want. Another thing is that how I can remove the ^M from a string. I found that there is a method “replace” in string class. But it will replace the whole string, right?

Have a look at the gsub method of String

Colin
