string.at(2,3) = "five";If the SubString starts at a position outside the length of the original string (like e.g. in after(1000000)), a zero length string is created (not an exception thrown like in standard string operations).
Assignment
The String class may be instantiated in many ways:
A String may be concatinated with another object (String, or char*) with either prepending or postpending. A search for the position of a character within a String may return its position, a Bool that it is contained within or a Bool confirming your guess at the character's position is correct. A check of the frequency of occurance of a string within a String will return the number of occurances.
Strings may be extracted from Strings at, before, through, from and after a starting position within the String. Deletion of characters is possible after a given position within the String. Global substitution of characters within a String is provided, as well. Splitting of Strings into a carray of Strings is possible, based upon a given separator character, with a return value of the number of elements split. The joining together of the elements of an array of Strings into one String is possible.
Finally, transformations of case and conversions of type are provided.
The standard string class provides the following functionality:
// Let's start with a simple string. String myString("the time"); // add some more on the end... myString += " for all good men"; // prepend some on the front... myString.prepend("Now is "); // do some concatination... String evenMore; evenMore += myString + " to come to"; // do some three way concatination String allKeys, finishIt(" their country."); allKeys = evenMore + "the aid of" + finishIt; // find the spot where we put something earlier String::size_type position = allKeys.index(finishIt); // find if the word is in the String... Bool query = myString.contains("good men"); // ask if the position we think is true is correct... Bool answer = allKeys.matches(finishIt, position); // How many spaces are in our phrase? Int spacesCount = allKeys.freq(" "); </scrblock> </example> <motivation> The String class eases the handling of characters within the AIPS++ environment. </motivation> <todo asof=2000/12/05"> <li> if old string disappeared; remove the alloc() call. <li> add more tests (for string methods) when old String disappears </todo>Member Description
String() : string("")
Default constructor
String(const string& str, size_type pos=0, size_type n=npos) : string(str, pos, n)
Construct from std string Construct from (part of) other string: acts as copy constructor
Thrown Exceptions
Construct from char* with given length
Construct from char array
Construct from a single char (repeated n times)
Construct from iterator
From single char (** aips++ addition).
Note that there is no automatic Char-to-String conversion available. This stops inadvertent conversions of integer to string.
Construct from a SubString
Construct from a stream.
Destructor
Assignments (they are all deep copies according to standard)
** aips++ addition: synonym for at(pos, len)
Indexing. The standard version is undefined if pos > size(), or pos >= size() for non-const version.
The const_reference version needs the at() version for the gcc compiler: no const [] exists.
*** aips++ addition
Iterators
Capacity, size
** aips++ addition -- works as a capacity(n) -- Note Int
Capacity, size
Resize by truncating or extending with copies of c (default Char())
** aips++ addition -- works as a resize(n)
Resize by truncating or extending with copies of c (default Char())
Clear the string
clear() executed as erase() due to missing clear() in gcc
Test for empty
Addressing
Append
The standard has a void push_back(const Char) which is completely undefined. It probably is a remnant of the full list of container functions pop/push back/front.
** aips++ addition
Append
The standard has a void push_back(const Char) which is completely undefined. It probably is a remnant of the full list of container functions pop/push back/front.
Assign
** aips++ addition
Assign
Insert
** aips++ addition
Insert
** aips++ additions
Compare. Returns 0 if strings equal and of equal size; else positive if str larger or longer; else negative.
The gcc compiler does not have the proper standard compare functions. Hence they are locally implemented.
Erase
Replace
** aips++ addition
Replace
** aips++ addition
Replace
Copy
Swap
Get char array
As a proper null terminated C-string
Get char array
As pointer to char array
Get char array
** aips++ synonym
Get allocator used
gcc has no get_allocator()
Get a sub string
Search functions. Returns either npos (if not found); else position.
The RegexBase ones are ** aips++ additions
Containment. ** aips++ addition
Matches entire string from pos
(or till pos if negative pos). ** aips++ addition
Concatenate by prepending the argument onto String. ** aips++ addition
Return the position of the target in the string or npos for failure.
** aips++ addition
Return the number of occurences of target in String. ** aips++ addition
Extract the string "at" the argument's position. ** aips++ addition
Start at startpos and extract the string "before" the argument's
position, exclusive. ** aips++ addition
Next one for overloading reasons
Start at startpos and extract the string "before" the argument's
position, exclusive. ** aips++ addition
Start at startpos and extract the SubString "through" to the argument's
position, inclusive. ** aips++ addition
Next one for overloading reasons
Start at startpos and extract the SubString "through" to the argument's
position, inclusive. ** aips++ addition
Start at startpos and extract the SubString "from" the argument's
position, inclusive, to the String's end. ** aips++ addition
Next one for overloading reasons
Start at startpos and extract the SubString "from" the argument's
position, inclusive, to the String's end. ** aips++ addition
Start at startpos and extract the SubString "after" the argument's
position, exclusive, to the String's end. ** aips++ addition
Next one for overloading reasons
Start at startpos and extract the SubString "after" the argument's
position, exclusive, to the String's end. ** aips++ addition
Maybe forget some. ** aips++ addition
internal transformation to capitalization of String.
Maybe forget some. ** aips++ addition
internal transformation to uppercase of String
Maybe forget some. ** aips++ addition
internal transformation to lowercase of String
Maybe forget some. ** aips++ addition
Delete len chars starting at pos. ** aips++ addition
Delete the first occurrence of target after startpos. ** aips++ addition
Overload problem
Delete the first occurrence of target after startpos. ** aips++ addition
Global substitution: substitute all occurrences of pat with repl, and
return the number of replacements.
** aips++ addition
Convert a integer to a String. This is a convenience function. Use the
ostringstream class for conversion of floating point data types
or more sophisticated formatting options.
The global concatenation operators
The global comparison operators
** aips++ additions of global compares. Returns 0 if equal; lt or gt 0 if
strings unequal or of unequal lengths.
this version ignores case. ** aips++ addition. Result is 0 if equal
strings of equal lengths; else lt or gt 0 to indicate differences.
** aips++ additions of global compares. Returns 0 if equal; lt or gt 0 if
strings unequal or of unequal lengths.
Global function which splits the String into string array res at separator
and returns the number of elements. ** aips++ addition
Functions to find special patterns, join and replicate
Case conversion and rearrangement functions
Bool contains(Char c, Int pos) const
Containment after (or before if pos negative) pos. ** aips++ addition
Bool contains(const string &str, Int pos) const
Bool contains(const Char *s, Int pos) const
Bool contains(const RegexBase &r, Int pos) const
Bool matches(const string &str, Int pos = 0) const
Bool matches(Char c, Int pos = 0) const
Bool matches(const Char *s, Int pos = 0) const
Bool matches(const RegexBase &r, Int pos = 0) const
void prepend(const string &str)
void prepend(const Char *s)
void prepend(Char c)
size_type index(Char c, Int startpos = 0) const
size_type index(const string &str, Int startpos = 0) const
size_type index(const Char *s, Int startpos = 0) const
size_type index(const RegexBase &r, Int startpos = 0) const
Int freq(Char c) const
Int freq(const string &str) const
Int freq(const Char *s) const
SubString at(size_type pos, size_type len)
String at(size_type pos, size_type len) const
SubString at(const string &str, Int startpos = 0)
String at(const string &str, Int startpos = 0) const
SubString at(const Char *s, Int startpos = 0)
String at(const Char *s, Int startpos = 0) const
SubString at(Char c, Int startpos = 0)
String at(Char c, Int startpos = 0) const
SubString at(const RegexBase &r, Int startpos = 0)
String at(const RegexBase &r, Int startpos = 0) const
SubString at(Int pos, Int len)
Next ones for overloading reasons.
String at(Int pos, Int len) const
SubString at(Int pos, size_type len)
String at(Int pos, size_type len) const
It is better to use the substr() method
in stead.
SubString before(Int pos)
SubString before(size_type pos)
SubString before(const string &str, Int startpos = 0)
SubString before(const Char *s, Int startpos = 0)
SubString before(Char c, Int startpos = 0)
SubString before(const RegexBase &r, Int startpos = 0)
SubString through(Int pos)
SubString through(size_type pos)
SubString through(const string &str, Int startpos = 0)
SubString through(const Char *s, Int startpos = 0)
SubString through(Char c, Int startpos = 0)
SubString through(const RegexBase &r, Int startpos = 0)
SubString from(Int pos)
SubString from(size_type pos)
SubString from(const string &str, Int startpos = 0)
SubString from(const Char *s, Int startpos = 0)
SubString from(Char c, Int startpos = 0)
SubString from(const RegexBase &r, Int startpos = 0)
SubString after(Int pos)
SubString after(size_type pos)
SubString after(const string &str, Int startpos = 0)
SubString after(const Char *s, Int startpos = 0)
SubString after(Char c, Int startpos = 0)
SubString after(const RegexBase &r, Int startpos = 0)
void capitalize()
void upcase()
void downcase()
void reverse()
void del(size_type pos, size_type len)
void del(Int pos, Int len)
void del(const string &str, size_type startpos = 0)
void del(const Char *s, size_type startpos = 0)
void del(Char c, size_type startpos = 0)
void del(const RegexBase &r, size_type startpos = 0)
Int gsub(const string &pat, const string &repl)
Int gsub(const Char *pat, const string &repl)
Int gsub(const Char *pat, const Char *repl)
Int gsub(const RegexBase &pat, const string &repl)
static String toString(Int value)
static String toString(uInt value)
SubString _substr(size_type first, size_type l)
Helper functions for at, before etc
Global concatenation operators (source)
Interface
Description
Member Description
inline String operator+(const String &lhs, const String &rhs)
inline String operator+(const Char *lhs, const String &rhs)
inline String operator+(Char lhs, const String &rhs)
inline String operator+(const String &lhs, const Char *rhs)
inline String operator+(const String &lhs, Char rhs)
Global comparison operators (source)
Interface
Description
Member Description
inline Bool operator==(const String &x, const String &y)
inline Bool operator!=(const String &x, const String &y)
inline Bool operator>(const String &x, const String &y)
inline Bool operator>=(const String &x, const String &y)
inline Bool operator<(const String &x, const String &y)
inline Bool operator<=(const String &x, const String &y)
inline Bool operator==(const String &x, const Char *t)
inline Bool operator!=(const String &x, const Char *t)
inline Bool operator>(const String &x, const Char *t)
inline Bool operator>=(const String &x, const Char *t)
inline Bool operator<(const String &x, const Char *t)
inline Bool operator<=(const String &x, const Char *t)
inline Bool operator==(const String &x, const Char t)
inline Bool operator!=(const String &x, const Char t)
inline Bool operator>(const String &x, const Char t)
inline Bool operator>=(const String &x, const Char t)
inline Bool operator<(const String &x, const Char t)
inline Bool operator<=(const String &x, const Char t)
Int fcompare(String x, String y)
inline Int compare(const string &x, const string &y)
inline Int compare(const string &x, const Char *y)
inline Int compare(const string &x, const Char y)
Splitting (source)
Interface
Description
Member Description
Int split(const string &str, string res[], Int maxn, const string &sep)
Int split(const string &str, string res[], Int maxn, const Char sep)
Int split(const string &str, string res[], Int maxn, const RegexBase &sep)
Some general functions (source)
Interface
Description
Member Description
String common_prefix(const string &x, const string &y, Int startpos = 0)
String common_suffix(const string &x, const string &y, Int startpos = -1)
String replicate(Char c, String::size_type n)
String replicate(const string &str, String::size_type n)
String join(string src[], Int n, const string &sep)
Casing and related functions (source)
Interface
Description
Member Description
String reverse(string str)
Global function which returns a transformation to reverse order of String.
String upcase(string str)
Global function which returns a transformation to uppercase of String.
String downcase(string str)
Global function which returns a transformation to lowercase of String.
String capitalize(string str)
Global function which returns a transformation to capitalization of
String.
IO (source)
Interface
Description
Member Description
ostream &operator<<(ostream &s, const String &x)
Output