Searching |
Top Previous Next |
Scripts > Class elements and c++ instructions > Interpreted C++ instructions > str > Searching
A group of methods can be used, to find certain positions inside of a string str.
unsigned int find(const str& xs) const unsigned int find(const str& xs, unsigned int pos) const unsigned int rfind(const str& xs) const unsigned int rfind(const str& xs, unsigned int pos) const unsigned int find_first_of(const str& xs) const unsigned int find_first_of(const str& xs, unsigned int pos) const unsigned int find_first_not_of(const str& xs) const unsigned int find_first_not_of(const str& xs, unsigned int pos) const unsigned int find_last_of(const str& xs) const unsigned int find_last_of(const str& xs, unsigned int pos) const unsigned int find_last_not_of(const str& xs) const unsigned int find_last_not_of(const str& xs, unsigned int pos) const
unsigned int find(const str& xs) const unsigned int find(const str& xs, unsigned int pos) const
Returns the index of the first occurrence of xs in the string, if there is one. Otherwise a special constant: str::npos, is returned. (str::npos only can be used in context of an equality operator.)
str s = "hello world"; unsigned int pos = s.find("o world"); if(pos != str::npos) out << s.substr(0, pos); // else don't use value of pos
Prints: hell
You can pass the position, from where the search shall start, as a second parameter to the find-function.
str s = "C:\\Programme\\TextTransformer\\Target\\test.txt"; unsigned int pos = s.find("\\"); unsigned int lastpos = str::npos; while(pos != str::npos) { lastpos = pos + 1; pos = s.find("\\", lastpos); } if(lastpos != str::npos) out << s.substr(0, lastpos); // prints : C:\Programme\TextTransformer\Target\
unsigned int rfind(const str& xs) const unsigned int rfind(const str& xs, unsigned int pos) const
The rfind methods are working analogously to the according find methods, but rfind searches backward. The result of the last example will be found faster by the rfind method:
str s = "C:\\Programme\\TextTransformer\\Target\\test.txt"; unsigned int pos = s.rfind("\\"); if(pos != str::npos) out << s.substr(0, pos + 1); // prints : C:\Programme\TextTransformer\Target\
unsigned int find_first_of(const str& xs) const unsigned int find_first_of(const str& xs, unsigned int pos) const unsigned int find_first_not_of(const str& xs) const unsigned int find_first_not_of(const str& xs, unsigned int pos) const unsigned int find_last_of(const str& xs) const unsigned int find_last_of(const str& xs, unsigned int pos) const unsigned int find_last_not_of(const str& xs) const unsigned int find_last_not_of(const str& xs, unsigned int pos) const
These functions search for characters of the string argument xs. By the optional second argument a position can be set, where the search shall start.
find_first_of searches the first character of xs
find_first_not_of searches the first character, which is not contained in xs
find_last_of searches backwards the first character of xs
find_last_not_of searches backwards the first character, which is not contained in xs
Example:
If you prcess the following code with xs = "Hello\r\nGood bye", you will get the result: "\"Hello\\r\\nGood bye\"". If the result is printed, you get the output: "Hello\r\nGood bye".
str sResult = "\""; str sFindWhat = "\\\r\n"; unsigned int oldpos = 0; unsigned int pos = xs.find_first_of(sFindWhat); while(pos != str::npos) { sResult += xs.substr(oldpos, pos - oldpos); switch(xs[pos]) { case '\\': sResult += "\\"; break; case '\r': sResult += "\\r"; break; case '\n': sResult += "\\n"; break; } oldpos = pos + 1; pos = xs.find_first_of(sFindWhat, oldpos); }
sResult += xs.substr(oldpos); sResult += "\""; return sResult;
|
This page belongs to the TextTransformer Documentation |
Home Content German |