next up previous contents index
Next: File Input Streams ( Up: Simple Data Types and Previous: Simple Data Types and   Contents   Index


Strings ( string )

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/core/string.h >

Types

string::size_type the size type.

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.

bool s.empty() returns whether s is the empty string.

char s.get(int i) returns the character at position i.
Precondition 0 < = i < = s.length()-1.

char& s[int i] returns a reference to the character at position i.
Precondition 0 < = i < = s.length()-1.

string s.substring(int i, int j) returns the substring of s starting at position max(0, i) and ending at position min(j - 1, s.length()-1).

string s.substring(int i) returns the substring of s starting at position max(0, i).

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 if i > = 0 and the first ( length() + i) characters of s if i < 0.

string s.tail(int i) returns the last i characters of s if i > = 0 and the last ( length() + i) characters of s if i < 0.

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(const string& s1) returns pos(s1, 0).

bool s.contains(const string& s1)
    true iff s1 is a substring of s.

bool s.starts_with(const string& s1)
    true iff s starts with s1.

bool s.ends_with(const string& s1)
    true iff s starts with s1.

string s.insert(int i, string s1)
    returns s(0, i - 1) + s1 + s(i, s.length()-1).

string s.replace(const string& s1, const string& s2, int i=1)
    returns the string created from s by replacing the i-th occurrence of s1 in s by s2.
Remark: The occurences of s1 in s are counted in a non-overlapping manner, for instance the string sasas contains only one occurence of the string sas.

string s.replace(int i, int j, const string& s1)
    returns the string created from s by replacing s(i, j) by s1.
Preconditioni < = j.

string s.replace(int i, const string& s1)
    returns the string created from s by replacing s[i] by s1.

string s.replace_all(const string& s1, const 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(const 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(const 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. (If delim is ' \ n' it is extracted from the stream, otherwise it remains there.)

void s.read(char delim = ' ') same as s.read(cin,delim).

void s.read_line(istream& I) same as s.read(I,' \ n').

void s.read_line() same as s.read_line(cin).

void s.read_file(istream& I) same as s.read(I,'EOF').

void s.read_file() same as s.read_file(cin).

string& s += const string& x appends x to s and returns a reference to s.

string const string& x + const string& y
    returns the concatenation of x and y.

bool const string& x == const string& y
    true iff x and y are equal.

bool const string& x != const string& y
    true iff x and y are not equal.

bool const string& x < const string& y
    true iff x is lexicographically smaller than y.

bool const string& x > const string& y
    true iff x is lexicographically greater than y.

bool const string& x <= const string& y
    returns (x < y) | (x = = y).

bool const string& x >= const string& y
    returns (x > y) | (x = = y).

istream& istream& I » string& s same as s.read(I,' ').

ostream& ostream& O « const string& 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()).


next up previous contents index
Next: File Input Streams ( Up: Simple Data Types and Previous: Simple Data Types and   Contents   Index