Passing String to a function that expects char *

	//EXPECTS CONST CHAR:
	//e.g. void my_function (const char *the_string);
	string s1;
	my_function(s1.c_str());
	//EXPECTS CHAR:
	//e.g. void my_function (char *the_string);
	string s1;
	my_function((char*)s1.c_str());

Pass Char array to a String

	my_string = string(my_char_array);

Pass char to string

	char InputChar = 'Q';
	string OutputString(1, InputChar);

Accessing characters in a string

	string my_string = "abcd";
	std::cout << my_string[0] << my_string[3] << std::endl;		//This outputs: ad

Remove Trailing Character

	if (!my_string.empty() && my_string[(my_string.length()-1)] == '\n')
		my_string.erase(my_string.length()-1);
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *