String
From CometPublic
[edit] String
A string can be created by a sequence of characters enclosed in quotes such as:
cout << "Hello World" << endl;
which creates the string "Hello world" and outputs using the predefined stream cout. Strings can also be assigned to variables such as:
string s = file.getLine();
which reads a line from the file.
Strings provide the following interface:
class String{
int length();
string concat(string s);
string prefix(int n);
string suffix(int n);
string substring(int i, int n);
bool equals(string s);
int toInt();
string[] split(string delim);
string strip(string s);
string lstrip(string s);
string rstrip(string s);
int find(string s, [int start, [int end]]);
}
[edit] Description
| method | Description |
|---|---|
| int length() | Returns the length of the string. |
| string concat(string s) | Returns a new string that is the concatenation of this and s. |
| string prefix(int n) | Returns a new string containing the first n characters of the string. |
| string suffix(int n) | Returns a new string containing the last n characters of the string. |
| string substring(int i, int n) | Returns a new string with the n characters starting at index i. |
| bool equals(string s) | Returns true iff the two strings are equal. |
| int toInt() | Returns the integer conversion of the string. |
| string[] split(string delim) | Splits the string at any occurrence of the given delimiter and returns this array. The resulting strings do not contain the delimiter and may be empty. Note that the delimiter is the complete sequence of symbol. The string "delim" should not be interpreted as a list of single character delimiters, but as a multi-character delimiter. |
| string strip(string s) | Strips the characters in s from the beginning and end of the string and returns the resulting substring. |
| string lstrip(string s) | Like strip but only strips from the left. |
| string rstrip(string s) | Like strip but only strips from the right. |
| int find(string s, [int start, [int end]]) | Returns the index of the first occurrence of s within the string between the indexes start and end-1 inclusive, default 0 and length. |

