#include <string>
//#include <string.h>   //(.h is used for C not C++)
#include <cstring>
using namespace std;   //You need this or you need to use std::string when declaring
#include <sstream>    //Lets you create strings using << for instance

Creating and using strings

	string result;
	string s1 = "hello ";
	string s2 = "world";
	result = s1 + s2;

	//or you could do this:
	result = s1;
	result += " ";
	result += s2;
If you get errors?

You may be defining the strings somewhere not allowed so instead try defining the strings separately at the top of the function, e.g.:

	string result;
	string s1 ;
	string s2 ;
	...
	s1 = "hello ";
	s2 = "world";
	result = s1 + s2;

Does string equal

	if (MyString == "ab cd")
	{
	}
switch (MyString) ?

No, you can’t use switch() on a string in c++

Passing String to a function that expects char *

	string s1;
	my_function(s1.c_str());

String Length

	int length = my_string.length();

Delete String

You don’t have to. When the string goes out of scope, it’s destructor will be called automatically and the memory will be freed. If you want to clear the string right now (without waiting until it goes out of scope) just use:

mystringname.clear().

Is String is set or not(null)?

	if (my_string.empty())
		// nothing in my_string

Get Section Of A String

	some_other_string = my_string.substr(8, string::npos);    //Copy from character 8 to the end
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 *