String functions:
The string class is a pre-defined class. Think of it as a type like int or double.)
Many pre-defined classes in C++ have functions associated with them, called methods or member functions of the class. Member functions define the operations that can be performed on [?by] objects of the class. We've already used several member functions of other classes: in Chapter 3, we introduced the iostream member functions cout.precision() and cout.width(). These member functions work on cout, which is an object of the iostream class. We also introduced the fstream member functions open() and close() which work on files, objects of the fstream class.
A string variable is an object of the string class. Among its most useful member functions are length(), substr(), find(), insert(), and erase().
The length of a string is the number of characters currently stored in the string. There are two member functions that will return this value: length() and size(). (They do the same thing.)
To call a member function, we use dot notation: object.method();
You’ve seen
cout.precision()
cout.width()
Now we’ll have
city.length()
Example:
string city=”Queens”;
cout << city.length();
Will print 6, the number of characters stored in city.
Post A Comment:
0 comments: