Definition
An instance s of the data type string is a sequence of characters (type char). The number of characters in the sequence is called the length of s. A string of length zero is called the empty string. Strings can be used wherever a C++ const char* string can be used.
Strings differ from the C++ type char* in several aspects: parameter passing by value and assignment works properly (i.e., the value is passed or assigned and not a pointer to the value) and strings offer many additional operations.
#include < LEDA/string.h >
Creation
string | s | introduces a variable s of type string. s is initialized with the empty string. |
string | s(const char* p) | introduces a variable s of type string. s is initialized with a copy of the C++ string p. |
string | s(char c) | introduces a variable s of type string. s is initialized with the one-character string ``c''. |
string | s(const char* format, ...) | |
introduces a variable s of type string. s is initialized with the string produced by printf(format,...). |
Operations
int | s.length() | returns the length of string s. |
char& | s[int i] | returns the character at position i.
Precondition 0 < = i < = s.length()-1. |
string | s(int i, int j) | returns the substring of s starting at
position max(0, i) and ending at
position min(j, s.length()-1).
If min(j, s.length() -1) < max(0, i) then the empty string is returned. |
string | s.head(int i) | returns the first i characters of s. |
string | s.tail(int i) | returns the last i characters of s. |
int | s.pos(string s1, int i) | returns the minimum j such that j > = i and s1 is a substring of s starting at position j (returns -1 if no such j exists). |
int | s.pos(string s1) | returns pos(s1, 0). |
string | s.insert(int i, string s1) | |
returns s(0, i - 1) + s1 + s(i, s.length()-1). | ||
string | s.replace(string s1, string s2, int i=1) | |
returns the string created from s by replacing the i-th occurrence of s1 in s by s2. | ||
string | s.replace(int i, int j, string s1) | |
returns the string created from s by replacing
s(i, j) by s1.
Preconditioni < = j. |
||
string | s.replace(int i, string s1) | |
returns the string created from s by replacing s[i] by s1. | ||
string | s.replace_all(string s1, string s2) | |
returns the string created from s by replacing
all occurrences of s1 in s by s2.
PreconditionThe occurrences of s1 in s do not overlap (it's hard to say what the function returns if the precondition is violated.). |
||
string | s.del(string s1, int i=1) | returns s.replace(s1,"", i). |
string | s.del(int i, int j) | returns s.replace(i, j,""). |
string | s.del(int i) | returns s.replace(i,""). |
string | s.del_all(string s1) | returns s.replace_all(s1,""). |
void | s.read(istream& I, char delim = ' ') | |
reads characters from input stream I into s until the first occurrence of character delim. | ||
void | s.read(char delim = ' ') | same as to s.read(cin,delim). |
void | s.read_line(istream& I) | same as to s.read(I,' \ n'). |
void | s.read_line() | same as to s.read_line(cin). |
void | s.read_file(istream& I) | same as to s.read(I,'EOF'). |
void | s.read_file() | same as to s.read_file(cin). |
string& | s += x | appends x to s and returns a reference to s. |
string | x + y | returns the concatenation of x and y. |
bool | x == y | true iff x and y are equal. |
bool | x != y | true iff x and y are not equal. |
bool | x < y | true iff x is lexicographically smaller than y. |
bool | x > y | true iff x is lexicographically greater than y. |
bool | x <= y | returns (x < y) | (x = = y). |
bool | x >= y | returns (x > y) | (x = = y). |
istream& | istream& I >> string& s | same as s.read(I,' '). |
ostream& | ostream& O << s | writes string s to the output stream O. |
Implementation
Strings are implemented by C++ character vectors. All operations involving the search for a pattern s1 in a string s take time O(s.lenght()*s1.length()), [ ] takes constant time and all other operations on a string s take time O(s.length()).