Return to Topic Menu | Computer Science Main Page | MathBits.com | Terms of Use

Member functions of the apstring class

 

length( )  - returns the number of characters contained within the string.
 - does not include the null character at the end of the string.
 - largest string length is 1024.

example:
apstring name = "Alexis";
cout<< name.length( );  // will display 6

 

substr(x, y)  - returns the part of the string that starts at position x and is y characters long.
 - remember that array subscripts begin at zero.

example:
apstring word = "reaction";
cout<<word.substr(2,3);  // will display act

 

find(somestring)  - returns the position (number) of the first occurrence of somestring in the string.

example:
apstring name = "Frederick";
cout<<name.find("red");   // will display 1

 

find(somechar)  - returns the position (number) of the first occurrence of somechar in your string.

example:
apstring word = "roadrunner";
cout<<word.find('e');   // will display 8

 

c_str( )  - returns a C-style (not C++) string needed when using strings with functions from other libraries.

example:
apstring number;
cout<< "Please enter one integer ";
cin >> number;
cout<< number << " times three will be "<< 3 * atoi(number.c_str( ) );

 

 

Operator member functions of
apstring class

 

Concatenation puts two strings together so that the second string follows immediately after the first string.  When used with numbers, + means add, but when used with strings, + means concatenate.

example:
apstring firstword = "apple";
apstring secondword = "fritter";
cout<< firstword + secondword;   //will show applefritter

 

It is possible to concatenate one string onto the end of another string.
Remember:  x += y means x = x + y.

example:
apstring firstword = "Wally";
apstring secondword = "Dog";
apstring newword = " "; 

newword += firstword;
newword += " ";
newword += secondword;

cout<<newword;   //  will display Wally Dog

 

 Return to Topic Menu | Computer Science Main Page | MathBits.com | Terms of Use