adding the string function example
This commit is contained in:
@@ -287,6 +287,13 @@ object. There are several ways of constructing string objects:
|
|||||||
|
|
||||||
This [example program](getline.cpp) is the starting point to most of your homeworks. It shows how you can read information from a file, and write information into another file.
|
This [example program](getline.cpp) is the starting point to most of your homeworks. It shows how you can read information from a file, and write information into another file.
|
||||||
|
|
||||||
|
## 1.16 Two Useful String Functions
|
||||||
|
|
||||||
|
- find()
|
||||||
|
- substr()
|
||||||
|
|
||||||
|
This [example program](strings.cpp) which uses these two string functions, can also be very useful in your homeworks.
|
||||||
|
|
||||||
<!--## 1.16 C++ vs. Java
|
<!--## 1.16 C++ vs. Java
|
||||||
|
|
||||||
- Standard C++ library std::string objects behave like a combination of Java String and StringBuffer objects. If you aren’t sure of how a std::string member function (or operator) will behave, check its semantics or try it on small examples (or both, which is preferable).
|
- Standard C++ library std::string objects behave like a combination of Java String and StringBuffer objects. If you aren’t sure of how a std::string member function (or operator) will behave, check its semantics or try it on small examples (or both, which is preferable).
|
||||||
|
|||||||
33
lectures/01_introduction/strings.cpp
Normal file
33
lectures/01_introduction/strings.cpp
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]){
|
||||||
|
std::string line("Why not change the world? Because I don't know how, do you know?");
|
||||||
|
|
||||||
|
int start = 0;
|
||||||
|
// starting from the position start, and search the string variable line,
|
||||||
|
// to find the first question mark.
|
||||||
|
int end = line.find("?", start);
|
||||||
|
int len = end - start;
|
||||||
|
// go from start to end, but exclude the character at end.
|
||||||
|
// when we use the substr(start, length) function on a std::string,
|
||||||
|
// the substring includes the character at the start position,
|
||||||
|
// and the length of the substring is length.
|
||||||
|
// It does not include the character at the position start + length.
|
||||||
|
std::string myString = line.substr(start, len);
|
||||||
|
|
||||||
|
// print myString to console.
|
||||||
|
std::cout << myString << std::endl;
|
||||||
|
|
||||||
|
start = end+1;
|
||||||
|
// with an updated start position,
|
||||||
|
// we now find the second question mark.
|
||||||
|
end = line.find("?", start);
|
||||||
|
len = end - start;
|
||||||
|
// go from start to end, but exclude the character at end.
|
||||||
|
myString = line.substr(start, len);
|
||||||
|
|
||||||
|
// print myString to console.
|
||||||
|
std::cout << myString << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user