简体   繁体   中英

How do I remove the first n lines from a string in Ruby?

One\n
Two\n
Three\n
Four\n

remove_lines(2) would remove the first two lines, leaving the string:

Three\n
Four\n

s.to_a[2..-1].join

>> s = "One\nTwo\nThree\nFour\n"
=> "One\nTwo\nThree\nFour\n"
>> s.to_a[2..-1].join
=> "Three\nFour\n"
class String

  def remove_lines(i)
    split("\n")[i..-1].join("\n")
  end

end

Calling "One\\nTwo\\nThree\\nFour\\n".remove_lines(2) would result in "Three\\nFour" . If you need the trailing "\\n" you need to extend this method accordingly.

s = "One\nTwo\nThree\nFour"

lines = s.lines
> ["One\n", "Two\n", "Three\n", "Four"]

remaining_lines = lines[2..-1]
> ["Three\n", "Four"]

remaining_lines.join
> "Three\nFour"
  • String#lines converts the string into an array of lines (retaining the new line character at the end of each string)
  • [2..-1] specifies the range of lines to return, in this case the third through the last
  • Array#join concatenates the lines back together, without any space (but since the lines still contain the new line character, we don't need a separator)

In one line:

s.lines[2..-1].join

I had a situation where I needed to support multiple platform EOLN (both \\r and \\n), and had success with the following:

split(/\r\n|\r|\n/, 2).last

Or the equivalent remove_lines :

def remove_lines(number_of_lines=1)
  split(/\r\n|\r|\n/, number_of_lines+1).last
end

Here is a pure regexp one-liner. Hypothetically it should be even faster than the elegant solution provided by @DigitalRoss:

n = 4 # number of lines
str.gsub(/\A(.*\n){#{n}}/,'')

If you know in advance how many line you want to cut (4 here):

str.gsub(/\A(.*\n){4}/,'')

And if you want to cut only one line:

str.gsub(/\A.*\n/,'')

In order to cut n lines from the tail:

gsub(/(\n.*){#{n}}\Z/,'')

This problem will remove the first two lines using regular expression.

Text = "One\nTwo\nThree\nFour"
Text = Text.gsub /^(?:[^\n]*\n){2}/, ''
# -----------------------------------^^  (2) Replace with nothing
# ----------------^^^^^^^^^^^^^^^^       (1) Detect first 2 lines
puts Text

I've just saw that the question is also about ' n ' lines not just two lines. 我刚刚看到问题也是' n '行不仅仅是两行。

So here is my new answer.

Lines_Removed = 2
Original_Text = "One\nTwo\nThree\nFour"
Result___Text = (Original_Text.gsub(Regexp.new("([^\n]*\n){%s}" % Lines_Removed), ''))
#                                               ^^^^^^^^^^^^^^                    ^^
# - (1) Detect first  lines -----++++++++++++++                    ||
# - (2) Replace with nothing -----------------------------------------------------++

puts Result___Text # Returns "Three\nFour"
def remove_lines(str, n)
  res = ""
  arr = str.split("\n")[n..(str.size-n)]
  arr.each { |i| res.concat(i + "\n")  }
  return res
end

a = "1\n2\n3\n4\n"
b = remove_lines(a, 2)
print b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM