在Ruby语言中,如何获取字符串中的行数?

时间:2023-02-07 15:06:00

In Ruby language, how can I get the number of lines in a string?

在Ruby语言中,如何获取字符串中的行数?

6 个解决方案

#1


50  

There is a lines method for strings which returns an Enumerator. Call count on the enumerator.

返回枚举器的字符串有一个line方法。调用者的呼叫计数。

str = "Hello\nWorld"
str.lines.count # 2

str = "Hello\nWorld\n" # trailing newline is ignored
str.lines.count # 2

The lines method was introduced in Ruby 1.8.7. If you're using an older version, checkout the answers by @mipadi and @Greg.

lines方法在Ruby 1.8.7中引入。如果您使用的是旧版本,请查看@mipadi和@Greg的答案。

#2


5  

One way would be to count the number of line endings (\n or \r\n, depending on the string), the caveat being that if the string does not end in a new line, you'll have to make sure to add one to your count. You could do so with the following:

一种方法是计算行结尾的数量(\ n或\ r \ n,取决于字符串),需要注意的是,如果字符串没有以新行结尾,则必须确保添加一个到你的数量。您可以使用以下内容执行此操作:

c = my_string.count("\n")
c += 1 unless c[-1,1] == "\n"

You could also just loop through the string and count the lines:

你也可以循环遍历字符串并计算行数:

c = 0
my_string.each { |line| c += 1 }

Continuing with that solution, you could get really fancy and use inject:

继续使用该解决方案,您可以获得真正的幻想并使用注入:

c = my_string.each.inject(0) { |count, line| count += 1 }

#3


4  

string".split("\n").size works nicely. I like that it ignores trailing new-lines if they don't contain content.

字符串“.split(”\ n“)。size非常适合。我喜欢它忽略了尾随的新行,如果它们不包含内容。

"Hello\nWorld\n".split("\n") # => ["Hello", "World"]
"hello\nworld\nfoo bar\n\n".split("\n").size # => 3

That might not be what you want, so use lines() as @Anurag suggested instead if you need to honor all new-lines.

这可能不是你想要的,所以如果需要尊重所有新行,请使用@Anurag建议的lines()。

"hello\nworld\nfoo bar\n\n".lines.count # => 4

#4


1  

"hello\nworld\nfoo bar\n\n".chomp.split("\n",-1).size # => 4

“你好\ nworld \ n \ nfoo bar \ n \ n”.chomp.split(“\ n”, - 1).size#=> 4

String#chomp gets rid of an end of line if it exists, and the -1 allows empty strings.

String#chomp如果存在则删除行尾,-1允许空字符串。

#5


1  

given a file object (here, in rails)

给定一个文件对象(这里,在rails中)

file = File.open(File.join(Rails.root, 'lib', 'file.json'))
file.readlines.count

returns the number of lines

返回行数

IO#readlines performs a split method on strings (IOStrings in this case) using newlines as the separator

IO#readlines使用换行符作为分隔符对字符串(在本例中为IOStrings)执行拆分方法

#6


1  

This will not count blank lines:

这不会算空行:

string.split("\n").select{ |line| line != "" }.size

#1


50  

There is a lines method for strings which returns an Enumerator. Call count on the enumerator.

返回枚举器的字符串有一个line方法。调用者的呼叫计数。

str = "Hello\nWorld"
str.lines.count # 2

str = "Hello\nWorld\n" # trailing newline is ignored
str.lines.count # 2

The lines method was introduced in Ruby 1.8.7. If you're using an older version, checkout the answers by @mipadi and @Greg.

lines方法在Ruby 1.8.7中引入。如果您使用的是旧版本,请查看@mipadi和@Greg的答案。

#2


5  

One way would be to count the number of line endings (\n or \r\n, depending on the string), the caveat being that if the string does not end in a new line, you'll have to make sure to add one to your count. You could do so with the following:

一种方法是计算行结尾的数量(\ n或\ r \ n,取决于字符串),需要注意的是,如果字符串没有以新行结尾,则必须确保添加一个到你的数量。您可以使用以下内容执行此操作:

c = my_string.count("\n")
c += 1 unless c[-1,1] == "\n"

You could also just loop through the string and count the lines:

你也可以循环遍历字符串并计算行数:

c = 0
my_string.each { |line| c += 1 }

Continuing with that solution, you could get really fancy and use inject:

继续使用该解决方案,您可以获得真正的幻想并使用注入:

c = my_string.each.inject(0) { |count, line| count += 1 }

#3


4  

string".split("\n").size works nicely. I like that it ignores trailing new-lines if they don't contain content.

字符串“.split(”\ n“)。size非常适合。我喜欢它忽略了尾随的新行,如果它们不包含内容。

"Hello\nWorld\n".split("\n") # => ["Hello", "World"]
"hello\nworld\nfoo bar\n\n".split("\n").size # => 3

That might not be what you want, so use lines() as @Anurag suggested instead if you need to honor all new-lines.

这可能不是你想要的,所以如果需要尊重所有新行,请使用@Anurag建议的lines()。

"hello\nworld\nfoo bar\n\n".lines.count # => 4

#4


1  

"hello\nworld\nfoo bar\n\n".chomp.split("\n",-1).size # => 4

“你好\ nworld \ n \ nfoo bar \ n \ n”.chomp.split(“\ n”, - 1).size#=> 4

String#chomp gets rid of an end of line if it exists, and the -1 allows empty strings.

String#chomp如果存在则删除行尾,-1允许空字符串。

#5


1  

given a file object (here, in rails)

给定一个文件对象(这里,在rails中)

file = File.open(File.join(Rails.root, 'lib', 'file.json'))
file.readlines.count

returns the number of lines

返回行数

IO#readlines performs a split method on strings (IOStrings in this case) using newlines as the separator

IO#readlines使用换行符作为分隔符对字符串(在本例中为IOStrings)执行拆分方法

#6


1  

This will not count blank lines:

这不会算空行:

string.split("\n").select{ |line| line != "" }.size