The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details. |
Home Strings and things The Find Function | ||
The Find Function
int index = fruit.find('a'); This example finds the index of the letter 'a' in the string. In this case, the letter appears three times, so it is not obvious what find should do. According to the documentation, it returns the index of the first appearance, so the result is 1. If the given letter does not appear in the string, find returns -1. In addition, there is a version of find that takes another pstring as an argument and that finds the index where the substring appears in the string. For example, pstring fruit = "banana";int index = fruit.find("nan"); This example returns the value 2. You should remember from Section 5.4 that there can be more than one function with the same name, as long as they take a different number of parameters or different types. In this case, C++ knows which version of find to invoke by looking at the type of the argument we provide.
|
||
Home Strings and things The Find Function |