The String package consists of the single class String
. This class provides ways to assign, concatenate, and compare
character strings. This class also provides methods for substring
creation and for vector access to a character string.
Provides the capabilities for manipulating sequences of characters.
#include <string.hxx>
#include <String.h>
class String
{
friend ostream &operator<<(ostream &, const String &);
friend istream &operator>>(istream &, String &);
friend int operator==(const String &, const String &);
friend int operator==(const String &, const char *);
friend int operator==(const char *, const String &);
friend int operator!=(const String &, const String &);
friend int operator!=(const String &, const char *);
friend int operator!=(const char *, const String &);
friend int operator<(const String &, const String &);
friend int operator<(const String &, const char *);
friend int operator<(const char *, const String &);
friend int operator>(const String &, const String &);
friend int operator>(const String &, const char *);
friend int operator>(const char *, const String &);
friend int operator<=(const String &, const String &);
friend int operator<=(const String &, const char *);
friend int operator<=(const char *, const String &);
friend int operator>=(const String &, const String &);
friend int operator>=(const String &, const char *);
friend int operator>=(const char *, const String &);
friend String operator+(const String &, const String &);
friend String operator+(const String &, const char *);
friend String operator+(const char *, const String &);
public:
String();
String(const String &);
String(const char *);
String(const char &);
~String();
String &operator=(const String &);
String &operator=(const char *);
operator char * () const;
operator const char * () const;
String &operator+=(const String &);
String &operator+=(const char *);
String operator()(int, int) const;
unsigned int length() const;
String upper() const;
String lower() const;
int match(const String &) const;
int index(const String &) const;
char operator[](int) const;
char &operator[](int);
};
char
. For some applications, the services provided are like those
provided by the traditional C string library (strcpy
, strcmp , and so forth), but are more efficient and
convenient in the context of C++. Overloaded operators provide ways
to assign, concatenate, and compare strings. New operators provide
simple notations for substring creation and vector access into the
string.
All comparisons are lexicographic, with the ordering dependent on the character set in which the string is encoded.
An index value of 0 indicates the first character in a
string object.
String object initialized to an empty
string. String object and initializes it to
the null-terminated sequence of characters. String object with a reference to a
char datum to initialize the string. String object with a reference
to another String to initialize the first
String . String object; no user action is
required. String object. String object with a null-terminated
sequence of characters. String object with another
String object. String object to a null-terminated
sequence of characters. String object to another
String object. String object; if so, it returns 1. Otherwise, it
returns 0. String object is less than a null-
terminated sequence of characters; if so, it returns 1. Otherwise,
it returns 0. String objects to determine if the
first is less than the second; if so, it returns 1. Otherwise, it
returns 0. String object; if so, it returns 1. Otherwise,
it returns 0. String object is greater than a null-
terminated sequence of characters; if so, it returns 1. Otherwise,
it returns 0. String objects to determine if the
first is greater than the second; if so, it returns 1. Otherwise, it
returns 0. String object. String object to another
String object. String object; if so, it returns 1. Otherwise, it
returns 0. String object is equal to a null-
terminated sequence of characters; if so, it returns 1. Otherwise,
it returns 0. String objects to determine equality.
If one is equal to the other, it returns 1; otherwise, it returns 0.
String object; if so, it returns 1. Otherwise, it
returns 0. String object is not equal to a null-
terminated sequence of characters; if so, it returns 1. Otherwise,
it returns 0. String objects to determine
inequality. If they are not equal, the function returns 1;
otherwise, it returns 0. String object; if so, it returns 1.
Otherwise, it returns 0. String object is less than or equal
to a null-terminated sequence of characters; if so, it returns 1.
Otherwise, it returns 0. String objects to determine if the
first is less than or equal to the second; if so, it returns 1.
Otherwise, it returns 0. String object; if so, it returns 1.
Otherwise, it returns 0. String object is equal to or greater
than a null-terminated sequence of characters; if so, it returns 1.
Otherwise, it returns 0. String objects to determine if the
first is equal to or greater than the second; if so, it returns 1.
Otherwise, it returns 0. String object defined as a substring
of the current String , with index as
the starting character and count as the length of the
substring. String object coincides with the value of
x. String object. String object constructed from
a String except that every character is lowercase
regardless of its original case. String argument can be a character pointer.
String constructed from a
String except that every character is uppercase
regardless of its original case.
String x ("The Times of John Doe");
char *y = "Pink Triangles";
if (x != y) cout << "We have two different strings.\n";
x = y;
cout << x;
The first line of this example provides a character string to the
constructor for initialization. The overloaded operators (!=,
<<, and =) accept either two String objects or a
String and a null-terminate sequence of characters.
The last line prints out the following character string:
Pink Triangles
String x ("The Times of John Doe");
String a (x(18,3)); // Substring is "Doe"
String b (x); // b contains all of x
In this example, the creation of object a provides
a substring of object x to the constructor for
object a . The substring begins at position 18 and
has a length of 3 characters. The next line creates the object
b and initializes it to contain the same value as
x .
String x ("World");
String y;
y = "Hello";
y += ", " + x + ".\n";
cout << y;
This example shows string concatenation. The last line prints out
the following message:
Hello, World.