I have a string, the string contains for example “Hello\nThis is a test.\n”.
I want to split the whole string on every \n in the string. I made this code already:
vector<string> inData = "Hello\nThis is a test.\n";
for ( int i = 0; i < (int)inData.length(); i++ )
{
if(inData.at(i) == "\n")
{
}
}
But when I complite this, then I get an error:
(\n as a string)
binary '==' : no operator found which takes a left-hand operand of type 'char' (or there is no acceptable conversion)
(above code)
'==' : no conversion from 'const char *' to 'int'
'==' : 'int' differs in levels of indirection from 'const char [2]'
The problem is that I can’t look if a char is equal to “new line”. How can I do this?
"\n"is aconst char[2]. Use'\n'instead.And actually, your code won’t even compile anyway.
You probably meant:
I removed the
vectorfrom your code because you apparently don’t want to use that (you were trying to initialize avector<string>from aconst char[], which will not work).Also notice the use of
size_tinstead of the conversion ofinData.length()toint.