In Ruby language, how can I get the number of lines in a string?
Ruby – In Ruby language, how can I get the number of lines in a string
countrubystring
Related Topic
- C++ – How to iterate over the words of a string
- Java – How to read / convert an InputStream into a String in Java
- Python – How to get a substring of a string in Python
- Ruby – How to write a switch statement in Ruby
- Javascript – How to make the first letter of a string uppercase in JavaScript
- Javascript – How to replace all occurrences of a string in JavaScript
- Javascript – How to check whether a string contains a substring in JavaScript
- Java – How to convert a String to an int in Java
Best Answer
There is a
lines
method for strings which returns anEnumerator
. Callcount
on the enumerator.The
lines
method was introduced in Ruby 1.8.7. If you're using an older version, checkout the answers by @mipadi and @Greg.