unsigned long num_tokens = count(const char* str, unsigned long start_position=0) const;
The count()
function will count the number of times that a substring is found in a
string. Note that characters that are counted for one substring are not
eligible for another. For example, count()
will find "AA" once
in "AAA", not twice.
Str x = "The dog jumped over the fence and ran into the house"; x.count("dog"); // returns 1 x.count("the"); // returns 2 x.count("The"); // returns 1 x.count("e"); // returns 8
long idx = findNext(const char* str, unsigned long start_position=0) const;
The findNext()
function will find the next occurrence of a
substring at or after start_position
and return the substring's starting index. If no occurrence is
found, then -1 is returned.
Str x = "The dog jumped over the fence and ran into the house"; // Find all of the string indexes where 'the' occurs long idx = 0; while ((idx = x.findNext("the", idx)) >= 0) { printf("Found at index %d\n", idx); idx += 3; // Skip over the current match }
unsigned long replace_count = replace(const char* old_str, const char* new_str, unsigned long start_position=0, bool replace_all=true);
The replace()
function can replace one or all occurrences of one
string with another string, starting at the given index. The number
returned is the number of substrings actually replaced. The
replace()
function may have to reallocate a new buffer if the new
string is larger than the old string's buffer.
Str x = "the man went to the store"; Str y = x; y.replace("the", "a"); // returns 2 // y = "a man went to a store" y = x; y.replace("the", "a", 3); // returns 1 // y = "the man went to a store" y = x; y.replace("the", "a", 0, false); // returns 1 // y = "a man went to the store"