String#count options

前端 未结 6 666
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

    Let's break these down

    a = "hello world"
    
    1. to count the number of occurrences of the letters l and o

      a.count "lo" #=> 5

    2. to find the intersect of lo and o (which is counting the number of occurrences of l and o and taking only the count of o from the occurrences):

      a.count "lo", "o" #=> 2

    3. to count the number of occurrences of h, e, l, l and o, then intersect with any that are not l (which produces the same outcome to finding occurrences of h, e and o)

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

    4. to count the number of occurrences of e and any letter between j and m (j, k, l and m):

      a.count "ej-m" #=> 4

提交回复
热议问题