O
O
Ord EO2020-04-12 16:09:15
ruby
Ord EO, 2020-04-12 16:09:15

How to count the number of elements in a string in ruby?

There is a line like this

f = '1232132131, 12321321, 34232423423, 324234234234'
You need to count the number of elements. I know that this option works for a list of words. How can I convert it into a version with numbers, or maybe there is an easier way?
f.split(/[^-a-zA-Z]/).size

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2020-04-12
@OrdeO

f = '1232132131, 12321321, 34232423423, 324234234234'
f.split.size ## 4
f.split(', ').size ## 4, вот так корректней

More details about the nuances: https://apidock.com/ruby/String/split

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question