Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String#count options

Tags:

string

ruby

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"            #=> 5 a.count "lo", "o"       #=> 2 a.count "hello", "^l"   #=> 4 a.count "ej-m"          #=> 4 

Any explanation will be helpful.

like image 343
Nick Vanderbilt Avatar asked Mar 14 '11 22:03

Nick Vanderbilt


People also ask

What are a strings?

1. A string is any series of characters that are interpreted literally by a script. For example, "hello world" and "LKJH019283" are both examples of strings. In computer programming, a string is attached to a variable as shown in the example below.

What is in a string?

A string is a data type used in programming, such as an integer and floating point unit, but is used to represent text rather than numbers. It is comprised of a set of characters that can also contain spaces and numbers. For example, the word "hamburger" and the phrase "I ate 3 hamburgers" are both strings.

What does string mean in C++?

One of the most useful data types supplied in the C++ libraries is the string. A string is a variable that stores a sequence of letters or other characters, such as "Hello" or "May 10th is my birthday!". Just like the other data types, to create a string we first declare it, then we can store a value in it.

What is string and why it is used?

Strings are like sentences. They are formed by a list of characters, which is really an "array of characters". Strings are very useful when communicating information from the program to the user of the program. They are less useful when storing information for the computer to use.


2 Answers

This is one of the dorkiest ruby methods, and pretty lousy documentation to boot. Threw me for a loop. I ended up looking at it because it looked like it should give me the count of occurrences of a given string. Nope. Not remotely close. But here is how I ended up counting string occurrences:

s="this is a string with is thrice" s.scan(/is/).count  # => 3 

Makes me wonder why someone asked for this method, and why the documentation is so lousy. Almost like the person documenting the code really did not have a clue as to the human-understandable "business" reason for asking for this feature.

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.

like image 84
Jon Kern Avatar answered Sep 22 '22 17:09

Jon Kern


If you pass more than 1 parameter to count, it will use the intersection of those strings and will use that as the search target:

a = "hello world" a.count "lo"            #=> finds 5 instances of either "l" or "o" a.count "lo", "o"       #=> the intersection of "lo" and "o" is "o", so it finds 2 instances a.count "hello", "^l"   #=> the intersection of "hello" and "everything that is not "l" finds 4 instances of either "h", "e" or "o" a.count "ej-m"          #=> finds 4 instances of "e", "j", "k", "l" or "m" (the "j-m" part) 
like image 24
edmz Avatar answered Sep 21 '22 17:09

edmz