如何从Ruby中的字符串中删除前n行?

时间:2022-09-15 13:06:36
One\n
Two\n
Three\n
Four\n

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

remove_lines(2)将删除前两行,留下字符串:

Three\n
Four\n

7 个解决方案

#1


42  

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"

#2


5  

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.

调用“One \ nTwo \ nThree \ nFour \ n”.remove_lines(2)将导致“Three \ nFour”。如果需要尾随“\ n”,则需要相应地扩展此方法。

#3


4  

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)
  • String#lines将字符串转换为行数组(在每个字符串的末尾保留新行字符)
  • [2..-1] specifies the range of lines to return, in this case the third through the last
  • [2 ..- 1]指定要返回的行的范围,在这种情况下是第三行到最后一行
  • 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)
  • Array#join将这些行重新连接在一起,没有任何空格(但由于这些行仍然包含新行字符,因此我们不需要分隔符)

In one line:

在一行中:

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

#4


3  

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

我有一种情况需要支持多个平台EOLN(\ r \ n和\ n),并取得了以下成功:

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

Or the equivalent remove_lines:

或等效的remove_lines:

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

#5


1  

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

EDIT: 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"

#6


1  

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

这是一个纯正的正则表达式单线程。假设它应该比@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):

如果您事先知道要切割多少条线(这里有4条):

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:

为了从尾部切割n行:

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

#7


0  

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

#1


42  

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"

#2


5  

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.

调用“One \ nTwo \ nThree \ nFour \ n”.remove_lines(2)将导致“Three \ nFour”。如果需要尾随“\ n”,则需要相应地扩展此方法。

#3


4  

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)
  • String#lines将字符串转换为行数组(在每个字符串的末尾保留新行字符)
  • [2..-1] specifies the range of lines to return, in this case the third through the last
  • [2 ..- 1]指定要返回的行的范围,在这种情况下是第三行到最后一行
  • 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)
  • Array#join将这些行重新连接在一起,没有任何空格(但由于这些行仍然包含新行字符,因此我们不需要分隔符)

In one line:

在一行中:

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

#4


3  

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

我有一种情况需要支持多个平台EOLN(\ r \ n和\ n),并取得了以下成功:

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

Or the equivalent remove_lines:

或等效的remove_lines:

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

#5


1  

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

EDIT: 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"

#6


1  

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

这是一个纯正的正则表达式单线程。假设它应该比@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):

如果您事先知道要切割多少条线(这里有4条):

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:

为了从尾部切割n行:

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

#7


0  

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