for each (std::string s in m_intro.text) // std::vector<string>
{
for (int i = 0; i < s.length(); i++)
{
char* chr = &s.at(i);
chr[i+1] = NULL;
std::string t(chr);
// other code is used below not shown as not relivent
}
}
I want to get a char from a string. Each char I get I then want to turn into a string (there is a function that needs a const std::string&)
The above code works once, but after the first loop the entire of s is null. I can see why this is happening.
What I want is to get the next char from s on each loop and store it as a string.
When the
charwas part of a C-string (orchararray), you used the right (if antiquated) approach of setting the next element toNULLto terminate the string.However in this case, this is not relevant; you’re just indexing into a
std::stringand replacing all its characters withNULL, which is of course not what you meant.std::stringhas a constructor you can use to avoid this nastiness:No need to mess with pointers or
chararrays orNULL-termination.