Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I strip tab characters from a string in Ruby?

Tags:

ruby

I have a program that loads some tab-separated lines into a MySQL table. One of the values has tabs in it, which is causing some problems. The data is created column by column, so I need to find a way to strip the tab character out of an individual field with gsub. I do not, however, want to get rid of anything else, like spaces.

like image 852
AKWF Avatar asked Jun 02 '11 15:06

AKWF


People also ask

How do you strip a string in Ruby?

Ruby has lstrip and rstrip methods which can be used to remove leading and trailing whitespaces respectively from a string. Ruby also has strip method which is a combination of lstrip and rstrip and can be used to remove both, leading and trailing whitespaces, from a string.

How do you remove all spaces from a string in Ruby?

gsub(/[[:space:]]/, '') - removes all whitespace, including unicode ones.

How do you delete a new line character in Ruby?

delete or . tr String method to delete the newlines.

How do you find the part of a string in Ruby?

Accessing Characters Within a String To print or work with some of the characters in a string, use the slice method to get the part you'd like. Like arrays, where each element corresponds to an index number, each of a string's characters also correspond to an index number, starting with the index number 0.


1 Answers

It's really easy \t is the tab character.

result = string.gsub /\t/, ''

or, in-place

string.gsub! /\t/, ''
like image 141
Kyle Sletten Avatar answered Oct 04 '22 23:10

Kyle Sletten