# Is this a bug of String#count?

**URL:** <https://www.ruby-forum.com/t/is-this-a-bug-of-string-count/193559>\
**Category:** Ruby\
**Created:** [August 19, 2010, 5:45pm UTC](https://www.ruby-forum.com/t/is-this-a-bug-of-string-count/193559 "2010-08-19T17:45:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lirh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/839c29/32.png) [@lirh](https://www.ruby-forum.com/u/lirh)\
**Post date:** [August 19, 2010, 5:45pm UTC](https://www.ruby-forum.com/t/is-this-a-bug-of-string-count/193559/1 "2010-08-19T17:45:24Z")

</div>

$ ruby -v  
ruby 1.9.1p429 (2010-07-02 revision 28523) [i386-darwin9]  
$ irb --simple-prompt

> > s = “a\u4e00” # \u4e00 is the chinese character for “one” in case you can’t read the next line  
> > =\> “aä¸€”
> 
> > s.encoding  
> > =\> #Encoding:UTF-8
> 
> > s.length  
> > =\> 2
> 
> > s.count("^a")  
> > =\> 0

Why the above result is 0 not 1? After all there are 2 characters  
in the string s. Is this a bug of String#count?

Thanks in advance.  
Ruohao

---

<div class="post-metadata">

**Author:** ![lirh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/839c29/32.png) [@lirh](https://www.ruby-forum.com/u/lirh)\
**Post date:** [August 19, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/is-this-a-bug-of-string-count/193559/2 "2010-08-19T17:51:21Z")

</div>

> > > s = “a\u4e00”

> > > s.count("^a")  
> > > =\> 0
> 
> Why the above result is 0 not 1? After all there are 2 characters  
> in the string s. Is this a bug of String#count?

count returns the sum of occurrences of characters. I don’t see any ^a  
in the original string…

---

<div class="post-metadata">

**Author:** ![lirh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/839c29/32.png) [@lirh](https://www.ruby-forum.com/u/lirh)\
**Post date:** [August 19, 2010, 5:56pm UTC](https://www.ruby-forum.com/t/is-this-a-bug-of-string-count/193559/3 "2010-08-19T17:56:17Z")

</div>

Roger P. wrote:

> > > > s = “a\u4e00”
> 
> > > > s.count("^a")  
> > > > =\> 0
> > 
> > Why the above result is 0 not 1? After all there are 2 characters  
> > in the string s. Is this a bug of String#count?
> 
> count returns the sum of occurrences of characters. I don’t see any ^a  
> in the original string…

But according to the documentation, “Any _other\_str_ that starts with a  
caret (^)  
is negated”, thus the following behavior:  
$ irb --simple-prompt

> > s = “abc”  
> > =\> “abc”
> 
> > s.count("^a")  
> > =\> 2  
> > There are two characters in s that is _not_ “a”.

---

<div class="post-metadata">

**Author:** ![lirh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/839c29/32.png) [@lirh](https://www.ruby-forum.com/u/lirh)\
**Post date:** [August 19, 2010, 5:58pm UTC](https://www.ruby-forum.com/t/is-this-a-bug-of-string-count/193559/4 "2010-08-19T17:58:18Z")

</div>

Ruohao Li wrote:

> Roger P. wrote:
> 
> > > > > s = “a\u4e00”
> > 
> > > > > s.count("^a")  
> > > > > =\> 0
> > > 
> > > Why the above result is 0 not 1? After all there are 2 characters  
> > > in the string s. Is this a bug of String#count?
> > 
> > count returns the sum of occurrences of characters. I don’t see any ^a  
> > in the original string…
> 
> But according to the documentation, “Any _other\_str_ that starts with a  
> caret (^)  
> is negated”, thus the following behavior:

Probably because non-ASCII aren’t counted as /\w/ matching anymore. You  
might want to ping core to see if it is expected or not.
