![]() ![]() ![]() ![]() |
![]() |
|
![]() |
||
![]() |
----------------------------------------------------------- String#count str.count([other_str] ) => fixnum ------------------------------------------------------------------------ Each _other_str_ parameter defines a set of characters to count. The intersection of these sets defines the characters to count in _str_. Any _other_str_ that starts with a caret (^) is negated. The sequence c1--c2 means all characters between c1 and c2. a = "hello world" a.count "lo" #=> 5 a.count "lo", "o" #=> 2 a.count "hello", "^l" #=> 4 a.count "ej-m" #=> 4
String#count --- count(str[, str2[, ... ) 文字列中にある、str に含まれる文字の数を返します。 str の形式は tr(1) [manual page] と同じです。つまり、 `a-c' は a から c を意味し、"^0-9" のように 文字列の先頭が `^' の場合は指定文字以外を意味します。 `-' は文字列の両端にない場合にだけ範囲指定の意味になります。 同様に、`^' もその効果は文字列の先頭にあるときだけです。また、 `-', `^', `\' はバックスラッシュ(`\')によ りエスケープすることができます。 引数を複数指定した場合は、すべての引数の積集合を意味します。 p 'abcdefg'.count('c') # => 1 p '123456789'.count('2378') # => 4 p '123456789'.count('2-8', '^4-6') # => 4 以下はこのメソッドの典型的な使用例で、ファイルの行数を返します (ただしファイルの末尾に改行が必要です)。 n_lines = File.open("foo").read.count("\n")