Section 2.29. Reversing a String


2.28. Counting Characters in Strings

The count method counts the number of occurrences of any of a set of specified characters:

s1 = "abracadabra" a  = s1.count("c")      # 1 b  = s1.count("bdr")    # 5


The string parameter is like a simple regular expression. If it starts with a caret, the list is negated:

c = s1.count("^a")      # 6 d = s1.count("^bdr")    # 6


A hyphen indicates a range of characters:

e = s1.count("a-d")     # 9 f = s1.count("^a-d")    # 2





The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net