next up previous contents
Next: Adding And Removing Data Up: Parsing and Formatting Previous: Parsing Groups   Contents

Subsections

Simple Tests

length

unsigned long length_of_string = inline length();

The length() function returns the length of the string, similar to the C function strlen(). Unlike strlen(), which requires $O(n)$ time to find an answer, length() requires only $O(1)$ time.

equals

bool is_equal = inline equals(const Str& str) const;
bool is_equal =        equals(const char* cstr) const;

The equals() function is similar to the strcmp() function except that equals() returns only true or false. The equals(const Str& str) form of the function will operate in $O(1)$ time if the strings are different lengths or a string is compared with itself. In other cases, the function has $O(n)$ efficiency.

startsWith, endsWith

bool starts_with = inline startsWith(const Str& str) const;
bool starts_with =        startsWith(const char* cstr) const;
bool ends_with   = inline endsWith(const char* cstr) const;
bool ends_with   = inline endsWith(const Str& str) const;

These functions return true if the given substring is at the beginning or the end of the string. For example:

Str x = "Hello";
x.startsWith("He");     // true
x.endsWith("o");        // true
x.startsWith("Hello");  // true
x.endsWith("Hello");    // true
x.startsWith("he");     // false
x.endsWith("tion");     // false

The startsWith() function will operate with $O(1)$ efficiency if a Str is passed to it and the substring is longer than the string compared against. In all other cases, these functions have $O(n)$ efficiency.



2007-05-05