Subscribe to 1-2-3-A-B-C++        RSS Feed
-----

String Objects

Icon Leave Comment
String objects help you work with a sequence of characters. Using the string object gives you a certain set of member functions that let you do things with the strings.

I found a list of its member functions at http://www.cplusplus.../string/string/
(Is there a place on DIC that shows references like this? I searched the board and would much rather link in site.)

To show off some of the functions of string, I've messed around with the program string_tester that is on page 78.

// string tester v2

#include <iostream>
#include <string>

using namespace std;

int main()
{
	// This block of code shows off the 3 methods to set a variable to string
	string word1 = "Dream";
	string word2("In");
	string word3 = "Code";
	string word4(3, '!');	// this method sets word4 to !!!
	
	string phrase = word1 + " " + word2 + " " + word3 + " " + word4;	// "strings" all the words together to make phrase
	cout << "The best place to learn is " << phrase << "\n\n";
	
	cout << "This phrase has " << phrase.size() << " characters in it.\n\n";  // lists the number of characters, including spaces, in phrase
	cout << "The character at position 10 is: " << phrase[10] << "\n\n";
	
	for (int i = 0; i < phrase.size(); ++i)
		cout << "Character at position " << i << " is: " << phrase[i] << endl;
	
	return 0;
	
}

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

May 2013

S M T W T F S
      1234
567891011
12131415161718
19 202122232425
262728293031 

Recent Entries

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)