1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
String objects may be manipulated by member functions but also by
operators. Using operators often results in more natural-looking code. In
cases where operators are available having equivalent functionality as member
function the operator is practically always preferred.
The following operators are available for tt(string) objects (in the examples
`object' and `argument' refer to existing tt(std::string) objects).
itemization(
it() plain assignment:
quote(a character, bf(C) or bf(C++) string may be assigned to a tt(string)
object. The assignment operator returns its left-hand side operand. Example:
verb(object = argument;
object = "C string";
object = 'x';
object = 120; // same as object = 'x')
)
it() addition:
quote(the arithmetic additive assignment operator and the addition
operator add text to a tt(string) object. The compound assignment operator
returns its left-hand side operand, the addition operator returns its result
in a temporary string object. When using the addition operator either the
left-hand side operand or the right-hand side operand must be a
tt(std::string) object. The other operand may be a char, a bf(C) string or a
bf(C++) string. Example:
verb(object += argument;
object += "hello";
object += 'x'; // integral expressions are OK
argument + otherArgument; // two std::string objects
argument + "hello"; // using + at least one
"hello" + argument; // std::string is required
argument + 'a'; // integral expressions are OK
'a' + argument;)
)
it() index operator:
quote(The index operator may be used to retrieve tt(object)'s
individual characters, or to assign new values to individual characters of a
non-const string object. There is no range-checking (use the tt(at()) member
function for that). This operator returns a tt(char &) or tt(char const &).
Example:
verb(object[3] = argument[5];)
)
it() logical operators:
quote(the logical comparison operators may be applied to two string
objects or to a string object and a bf(C) string to compare their
content. These operators return a tt(bool) value.
The tt(==, !=, >, >=, <,) and tt(<=) operators are available. The ordering
operators perform a lexicographical comparison of their content
using the ASCII character collating sequence. Example:
verb(object == object; // true
object != (object + 'x'); // true
object <= (object + 'x'); // true)
)
it() stream related operators:
quote(the insertion-operator (cf. section ref(CoutCinCerr)) may be used to
insert a tt(string) object into an tt(ostream), the extraction-operator may be
used to extract a string object from an tt(istream). The extraction operator
by default first ignores all whitespace characters and then extracts all
consecutively non-blank characters from an tt(istream). Instead of a string a
character array may be extracted as well, but the advantage of using a string
object should be clear: the destination string object is automatically resized
to the required number of characters. Example:
verb(cin >> object;
cout << object;)
)
)
|