str |
Top Previous Next |
Scripts > Class elements and c++ instructions > Interpreted C++ instructions > str
Syntax
str <identifier> ;
Description
str is the interpreter version of std::string or std::wstring. In the generated code str is defined by: typedef std::string str or, for unicode parser by: typedef std::wstring str.
A str variable contains chains of characters, that means: text. The control characters, which consist of the combination of a backslash with other characters also counts as a character, e.g. the linefeed '\n '. To use a backslash character in its literal meaning in a string, a second backslash has to be put in front of it: '\\'
Example:
str s = "C:\\TextTransformer\\bin"; // wrong: "C:\TextTransformer\bin"
The visual representation of other nongraphic characters is possible by escape sequences.
String literals adjacent to each other are unified into one single string literal. So you can better write longer texts.
Example:
str s = "/**************************\n" "* This is a comment *\n" "**************************/\n";
In contrast to the previous variable types, str is not a basic type, but a derived type (a class), which has methods, by which you can get information about the contained text or by which you can manipulate it:
a) Information:
bool empty() const returns true, if there is no text in the string unsigned int size() const returns the number of characters in the string unsigned int length() const returns the number of characters in the string
A whole family of methods is used to search for special positions inside of a string.
Parts of the string:
str substr(int from, int count) const str substr(int from) const
returns the part of the string beginning at the character with the index from and of the length count (or the rest of the text, if there aren't count characters any more). If there is no second parameter, all characters beginning at from will be returned.
Subscript-operator
Single characters of a string can be read by means of their index. The first character has index 0 and the last index size() - 1.
str s = "TextTransformer"; char c = s[5]; // now is: c == 'r'
It is not possible to change a character accessed by its index. Use the replace-method instead (see below)
b) Manipulation:
void clear() removes the text in the string str& replace(unsigned int pos, unsigned int len, const str& s)
Replaces (at most) len characters starting with position pos with all characters of s. A reference of the string itself is returned.
Example:
str s = "wood"; out << s.replace(0, 1, "g") << " "; out << s;
Output: good good |
This page belongs to the TextTransformer Documentation |
Home Content German |