Ruby: Remove Whitespace Chars at the Beginning of a String

Ruby: Remove whitespace chars at the beginning of a string

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.

Removing all whitespace from a string in Ruby

You can use something like:

var_name.gsub!(/\s+/, '')

Or, if you want to return the changed string, instead of modifying the variable,

var_name.gsub(/\s+/, '')

This will also let you chain it with other methods (i.e. something_else = var_name.gsub(...).to_i to strip the whitespace then convert it to an integer). gsub! will edit it in place, so you'd have to write var_name.gsub!(...); something_else = var_name.to_i. Strictly speaking, as long as there is at least one change made,gsub! will return the new version (i.e. the same thing gsub would return), but on the chance that you're getting a string with no whitespace, it'll return nil and things will break. Because of that, I'd prefer gsub if you're chaining methods.

gsub works by replacing any matches of the first argument with the contents second argument. In this case, it matches any sequence of consecutive whitespace characters (or just a single one) with the regex /\s+/, then replaces those with an empty string. There's also a block form if you want to do some processing on the matched part, rather than just replacing directly; see String#gsub for more information about that.

The Ruby docs for the class Regexp are a good starting point to learn more about regular expressions -- I've found that they're useful in a wide variety of situations where a couple of milliseconds here or there don't count and you don't need to match things that can be nested arbitrarily deeply.

As Gene suggested in his comment, you could also use tr:

var_name.tr(" \t\r\n", '')

It works in a similar way, but instead of replacing a regex, it replaces every instance of the nth character of the first argument in the string it's called on with the nth character of the second parameter, or if there isn't, with nothing. See String#tr for more information.

Remove whitespace from a string and then convert to integer

As the documentation says, /\s/ is equivalent to the character class /[ \t\r\n\f\v]/. In other words, /\s/ is a space, a tab, carriage return, a newline, a form feed, or a vertical tab.

You, however, have a U+00A0 NO-BREAK SPACE character, which is not in the list of the characters matching /\s/.

A better alternative would be to match against a Unicode Character Property such as /\p{Blank}/ or /\p{Space}/.

Ruby: Split, then remove leading/trailing whitespace in place?

s = "one thing, two things, three things, four things"
s.split(",").map(&:strip)
# => ["one thing", "two things", "three things", "four things"]

In my Ubuntu 13.04 OS,using Ruby 2.0.0p0

require 'benchmark'

s = "one thing, two things, three things, four things"
result = ""

Benchmark.bmbm do |b|
b.report("strip/split: ") { 1_000_000.times {result = s.split(",").map(&:strip)} }
b.report("regex: ") { 1_000_000.times {result = s.split(/\s*,\s*/)} }
end

Rehearsal -------------------------------------------------
strip/split: 6.260000 0.000000 6.260000 ( 6.276583)
regex: 7.310000 0.000000 7.310000 ( 7.320001)
--------------------------------------- total: 13.570000sec

user system total real
strip/split: 6.350000 0.000000 6.350000 ( 6.363127)
regex: 7.290000 0.000000 7.290000 ( 7.302163)

How do I remove leading whitespace chars from Ruby HEREDOC?

The <<- form of heredoc only ignores leading whitespace for the end delimiter.

With Ruby 2.3 and later you can use a squiggly heredoc (<<~) to suppress the leading whitespace of content lines:

def test
<<~END
First content line.
Two spaces here.
No space here.
END
end

test
# => "First content line.\n Two spaces here.\nNo space here.\n"

From the Ruby literals documentation:

The indentation of the least-indented line will be removed from each
line of the content. Note that empty lines and lines consisting solely
of literal tabs and spaces will be ignored for the purposes of
determining indentation, but escaped tabs and spaces are considered
non-indentation characters.

Delete all the whitespaces that occur after a word in ruby

There are numerous ways this could be accomplished without without a regular expressions, but using them could be the "cleanest" looking approach without taking sub-strings, etc. The regular expression I believe you are looking for is /(?!^)(\s)/.

"  hello world! How is it going?".gsub(/(?!^)(\s)/, '')
#=> " helloworld!Howisitgoing?"

The \s matched any whitespace character (including tabs, etc), and the ^ is an "anchor" meaning the beginning of the string. The ! indicates to reject a match with following criteria. Using those together to your goal can be accomplished.

If you are not familiar with gsub, it is very similar to replace, but takes a regular expression. It additionally has a gsub! counter-part to mutate the string in place without creating a new altered copy.

Note that strictly speaking, this isn't all whitespace "after a word" to quote the exact question, but I gathered from your examples that your intentions were "all whitespace except beginning of string", which this will do.

Ruby how to remove a more than one space character?

You can use gsub to replace one or more whitespace (regex / +/) to a single whitespace:

'I want to     learn ruby more and more.'.gsub(/ +/, " ")
#=> "I want to learn ruby more and more."

Remove trailing spaces from each line in a string in ruby

str.split("\n").map(&:rstrip).join("\n")

Its probably doable with regex but I prefer this way.

How to remove whitespace from string in Ruby

It looks like you have some non-breaking space characters in your file (C2 A0 is the UTF-8 encoding for U+00A0, which is non breaking space).

In Ruby’s regular expressions \s doesn’t match non-breaking spaces, which is why your code doesn’t appear to work (strip also doesn’t remove them). You can use the \p{Space} character property instead, or the POSIX bracket expression [[:space:]].

day_to = day_to.gsub(/\p{Space}/,'') unless day_to.nil?

or

day_to = day_to.gsub(/[[:space:]]/,'') unless day_to.nil?


Related Topics



Leave a reply



Submit