String#count options

前端 未结 6 766
Happy的楠姐
Happy的楠姐 2021-02-02 06:01

From the documentation for String#count I understand the first example, but I do not understand the rest of the examples:

a = \"hello world\"
a.count \"lo\"              


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-02 06:35

    Each argument defines a set of characters. The intersection of those sets determines the overall set that count uses to compute a tally.

    a = "hello world"
    
    a.count "lo"            # l o       => 5
    a.count "lo", "o"       # o         => 2
    

    And ^ can be used for negation (all letters in hello, except l)

    a.count "hello", "^l"   # h e o     => 4
    

    Ranges can be defined with -:

    a.count "ej-m"          # e j k l m => 4
    

提交回复
热议问题