Module: Essential Tools Module Group: String Processing Classes
Does not inherit
#include <rw/cstring.h> RWCString s("test string"); s(6,3); // "tri"
The class RWCSubString allows some subsection of an RWCString to be addressed by defining a starting position and an extent. For example the 7th through the 11th elements, inclusive, would have a starting position of 7 and an extent of 5. The specification of a starting position and extent can also be done in your behalf by such functions as RWCString::strip() or the overloaded function call operator taking a regular expression as an argument. There are no public constructors -- RWCSubStrings are constructed by various functions of the RWCString class and then destroyed immediately.
A zero length substring is one with a defined starting position and an extent of zero. It can be thought of as starting just before the indicated character, but not including it. It can be used as an lvalue. A null substring is also legal and is frequently used to indicate that a requested substring, perhaps through a search, does not exist. A null substring can be detected with member function isNull(). However, it cannot be used as an lvalue.
NOTE -- Member function overloads with std::string will only appear when building on top of the C++ Standard Library.
None
#include <iostream> #include <rw/cstring.h> int main() { RWCString s ("What I tell you is true."); std::cout << "Take the string: [" << s << "]\n"; // Create a substring and use it as an lvalue: s(16, 0) = "three times "; std::cout << "After assigning text to a substring, it becomes: [" << s << "]" << std::endl; return 0; }
Program output:
Take the string: [What I tell you is true.] After assigning text to a substring, it becomes: [What I tell you three times is true.]
RWCSubString& operator=(const RWCString&);
Assignment from an RWCString. The statements:
RWCString a; RWCString b; ... b(2, 3) = a;
will copy a's data into the substring b(2,3). The number of elements need not match: if they differ, b will be resized appropriately. Sets self's extent to be the length of the assigned RWCString. If self is the null substring, then the statement has no effect. Returns a reference to self.
RWCSubString& operator=(const std::string& str);
Assignment from an std::string. Returns reference to self.
RWCSubString& operator=(const RWCSubString&); RWCSubString& operator=(const RWCConstSubString&);
Assignment from an RWCSubString or an RWCConstSubString, respectively. The statements:
RWCString a; RWCString b; ... b(2, 3) = a(5,5);
copy 5 characters of a's data into the substring b(2,3). The number of elements need not match: if they differ, b will be resized appropriately. Sets self's extent to be the extent of the assigned RWCSubString or RWCConstSubString. If self is the null substring, then the statement has no effect. Returns a reference to self.
RWCSubString& operator=(const char*);
Assignment from a character string. Example:
RWCString str("Mary had a lamb"); char dat[] = "Perrier"; str(11,4) = dat; // "Mary had a Perrier"
Note that the number of characters selected need not match: if they differ, str will be resized appropriately. Sets self's extent to be the strlen() of the assigned character string. If self is the null substring, then the statement has no effect. Returns a reference to self.
char& operator[](size_t i); char operator[](size_t i) const;
Returns the ith character of the substring. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the length of the substring, less one. Bounds checking is performed: if the index is out of range, then an exception of type RWBoundsErr will occur.
char& operator()(size_t i); char operator()(size_t i) const;
Returns the ith character of the substring. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the length of the substring, less one. Bounds checking is enabled by defining the pre-processor macro RWBOUNDS_CHECK before including <rw/cstring.h>. In this case, if the index is out of range, then an exception of type RWBoundsErr will occur.
bool isNull() const;
Returns true if this is a null substring.
size_t length() const;
Returns the extent (i.e., length) of the RWCSubString.
bool operator!() const;
Returns true if this is a null substring.
size_t start() const;
Returns the starting element of the RWCSubString.
void toLower();
Changes all upper-case letters in self to lower-case. Uses the Standard C Library function tolower().
void toUpper();
Changes all lower-case letters in self to upper-case. Uses the Standard C Library function toupper().
bool operator==(const RWCSubString&, const RWCSubString&); bool operator==(const RWCString&, const RWCSubString&); bool operator==(const RWCSubString&, const RWCString&); bool operator==(const char*, const RWCSubString&); bool operator==(const RWCSubString&, const char*);
Returns true if the substring is lexicographically equal to the character string or RWCString argument. Case sensitivity is exact.
bool operator!=(const RWCString&, const RWCString&); bool operator!=(const RWCString&, const RWCSubString&); bool operator!=(const RWCSubString&, const RWCString&); bool operator!=(const char*, const RWCString&); bool operator!=(const RWCString&, const char*);
Returns the negation of the respective operator==().
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.