在Ruby中计算字符串中的所有字符

时间:2022-08-22 12:50:29

Is there an equivalent to PHP's strlen method in Ruby?

在Ruby中有相当于PHP的strlen方法吗?

I know about Ruby's String#count method, but it requires that I define a set of characters to count. In my situation, I want to count ALL characters, not just certain characters.

我知道Ruby的String#count方法,但它要求我定义一组字符来计算。在我的情况下,我想要计算所有字符,而不仅仅是某些字符。

1 个解决方案

#1


11  

Use String#size or String#length method. It will work for you.

使用String#size或String #length方法。它会对你有用。

Returns the character length of str.

返回str的字符长度。

Example :

"abc 12-".size  # => 7

#1


11  

Use String#size or String#length method. It will work for you.

使用String#size或String #length方法。它会对你有用。

Returns the character length of str.

返回str的字符长度。

Example :

"abc 12-".size  # => 7