Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Remove whitespace chars at the beginning of a string

Edit: I solved this by using strip! to remove leading and trailing whitespaces as I show in this video. Then, I followed up by restoring the white space at the end of each string the array by iterating through and adding whitespace. This problem varies from the "dupe" as my intent is to keep the whitespace at the end. However, strip! will remove both the leading and trailing whitespace if that is your intent. (I would have made this an answer, but as this is incorrectly marked as a dupe, I could only edit my original question to include this.)

I have an array of words where I am trying to remove any whitespace that may exist at the beginning of the word instead of at the end. rstrip! just takes care of the end of a string. I want whitespaces removed from the beginning of a string.

example_array = ['peanut', ' butter', 'sammiches']
desired_output = ['peanut', 'butter', 'sammiches']

As you can see, not all elements in the array have the whitespace problem, so I can't just delete the first character as I would if all elements started with a single whitespace char.

Full code:

words = params[:word].gsub("\n", ",").delete("\r").split(",")
words.delete_if {|x| x == ""}
words.each do |e|
  e.lstrip!
end

Sample text that a user may enter on the form:

Corn on the cob,
Fibonacci,
StackOverflow
Chat, Meta, About
Badges
Tags,,
Unanswered
Ask Question
like image 698
Melanie Shebel Avatar asked Apr 29 '11 07:04

Melanie Shebel


2 Answers

String#lstrip (or String#lstrip!) is what you're after.

desired_output = example_array.map(&:lstrip)

More comments about your code:

  1. delete_if {|x| x == ""} can be replaced with delete_if(&:empty?)
  2. Except you want reject! because delete_if will only return a different array, rather than modify the existing one.
  3. words.each {|e| e.lstrip!} can be replaced with words.each(&:lstrip!)
  4. delete("\r") should be redundant if you're reading a windows-style text document on a Windows machine, or a Unix-style document on a Unix machine
  5. split(",") can be replaced with split(", ") or split(/, */) (or /, ?/ if there should be at most one space)

So now it looks like:

words = params[:word].gsub("\n", ",").split(/, ?/)
words.reject!(&:empty?)
words.each(&:lstrip!)

I'd be able to give more advice if you had the sample text available.

Edit: Ok, here goes:

temp_array = text.split("\n").map do |line|
  fields = line.split(/, */)
  non_empty_fields = fields.reject(&:empty?)
end
temp_array.flatten(1)

The methods used are String#split, Enumerable#map, Enumerable#reject and Array#flatten.

Ruby also has libraries for parsing comma seperated files, but I think they're a little different between 1.8 and 1.9.

like image 157
Andrew Grimm Avatar answered Oct 16 '22 19:10

Andrew Grimm


> ' string '.lstrip.chop
=> "string"

Strips both white spaces...

like image 33
Amir Mehler Avatar answered Oct 16 '22 19:10

Amir Mehler