|
| 1 | +Returns the total number of characters in +self+ that are specified by the given selectors. |
| 2 | + |
| 3 | +For one 1-character selector, |
| 4 | +returns the count of instances of that character: |
| 5 | + |
| 6 | + s = 'abracadabra' |
| 7 | + s.count('a') # => 5 |
| 8 | + s.count('b') # => 2 |
| 9 | + s.count('x') # => 0 |
| 10 | + s.count('') # => 0 |
| 11 | + |
| 12 | + s = 'тест' |
| 13 | + s.count('т') # => 2 |
| 14 | + s.count('е') # => 1 |
| 15 | + |
| 16 | + s = 'よろしくお願いします' |
| 17 | + s.count('よ') # => 1 |
| 18 | + s.count('し') # => 2 |
| 19 | + |
| 20 | +For one multi-character selector, |
| 21 | +returns the count of instances for all specified characters: |
| 22 | + |
| 23 | + s = 'abracadabra' |
| 24 | + s.count('ab') # => 7 |
| 25 | + s.count('abc') # => 8 |
| 26 | + s.count('abcd') # => 9 |
| 27 | + s.count('abcdr') # => 11 |
| 28 | + s.count('abcdrx') # => 11 |
| 29 | + |
| 30 | +Order and repetition do not matter: |
| 31 | + |
| 32 | + s.count('ba') == s.count('ab') # => true |
| 33 | + s.count('baab') == s.count('ab') # => true |
| 34 | + |
| 35 | +For multiple selectors, |
| 36 | +forms a single selector that is the intersection of characters in all selectors |
| 37 | +and returns the count of instances for that selector: |
| 38 | + |
| 39 | + s = 'abcdefg' |
| 40 | + s.count('abcde', 'dcbfg') == s.count('bcd') # => true |
| 41 | + s.count('abc', 'def') == s.count('') # => true |
| 42 | + |
| 43 | +In a character selector, three characters get special treatment: |
| 44 | + |
| 45 | +- A caret (<tt>'^'</tt>) functions as a _negation_ operator |
| 46 | + for the immediately following characters: |
| 47 | + |
| 48 | + s = 'abracadabra' |
| 49 | + s.count('^bc') # => 8 # Count of all except 'b' and 'c'. |
| 50 | + |
| 51 | +- A hyphen (<tt>'-'</tt>) between two other characters defines a _range_ of characters: |
| 52 | + |
| 53 | + s = 'abracadabra' |
| 54 | + s.count('a-c') # => 8 # Count of all 'a', 'b', and 'c'. |
| 55 | + |
| 56 | +- A backslash (<tt>'\'</tt>) acts as an escape for a caret, a hyphen, |
| 57 | + or another backslash: |
| 58 | + |
| 59 | + s = 'abracadabra' |
| 60 | + s.count('\^bc') # => 3 # Count of '^', 'b', and 'c'. |
| 61 | + s.count('a\-c') # => 6 # Count of 'a', '-', and 'c'. |
| 62 | + 'foo\bar\baz'.count('\\') # => 2 # Count of '\'. |
| 63 | + |
| 64 | +These usages may be mixed: |
| 65 | + |
| 66 | + s = 'abracadabra' |
| 67 | + s.count('a-cq-t') # => 10 # Multiple ranges. |
| 68 | + s.count('ac-d') # => 7 # Range mixed with plain characters. |
| 69 | + s.count('^a-c') # => 3 # Range mixed with negation. |
| 70 | + |
| 71 | +For multiple selectors, all forms may be used, including negations, ranges, and escapes. |
| 72 | + |
| 73 | + s = 'abracadabra' |
| 74 | + s.count('^abc', '^def') == s.count('^abcdef') # => true |
| 75 | + s.count('a-e', 'c-g') == s.count('cde') # => true |
| 76 | + s.count('^abc', 'c-g') == s.count('defg') # => true |
| 77 | + |
| 78 | +Related: see {Querying}[rdoc-ref:String@Querying]. |
0 commit comments