C
Christopher Pisz
Is std::string::npos always going to be less than any std::string 's size()?
I am trying to handle a replacement of all occurances of a substr, in which
the replacement also contains the substr. Yick. All I could come up with is:
#include <string>
int main()
{
std::string text;
text = "\nThis is a test line with newlines\n<-here and \n<-here and
\n\n<-two here";
// Format the text
std::string formattedText(text);
// Change every occurrance of "\n" to "\r\n"
std::string::size_type index = 0;
do
{
index = formattedText.find('\n', index);
if( index != std::string::npos )
{
formattedText.insert(index, "\r");
index += 2;
}
} while( index < formattedText.size() );
// debugging contents of formattedText here
return 0;
}
However, it depends on std::string::npos always being less than the size of
the string and I am not certain whether that is safe or not.
I am trying to handle a replacement of all occurances of a substr, in which
the replacement also contains the substr. Yick. All I could come up with is:
#include <string>
int main()
{
std::string text;
text = "\nThis is a test line with newlines\n<-here and \n<-here and
\n\n<-two here";
// Format the text
std::string formattedText(text);
// Change every occurrance of "\n" to "\r\n"
std::string::size_type index = 0;
do
{
index = formattedText.find('\n', index);
if( index != std::string::npos )
{
formattedText.insert(index, "\r");
index += 2;
}
} while( index < formattedText.size() );
// debugging contents of formattedText here
return 0;
}
However, it depends on std::string::npos always being less than the size of
the string and I am not certain whether that is safe or not.