How to find the positions of '\n' within a string object [modified]
-
I have a string object called str. The content within is "Line1Line1\nLine2Line2\n". I can find the positions of '\n's using char array and use them like this str[10], but how to do this with string object. What is the function for this? I mean I know about string.find() and use it like this: int n = 0; n = str.find("\n"); but what if I want the second '\n'?
modified on Thursday, October 21, 2010 3:28 PM
-
I have a string object called str. The content within is "Line1Line1\nLine2Line2\n". I can find the positions of '\n's using char array and use them like this str[10], but how to do this with string object. What is the function for this? I mean I know about string.find() and use it like this: int n = 0; n = str.find("\n"); but what if I want the second '\n'?
modified on Thursday, October 21, 2010 3:28 PM
What is "string object"? CString? std::string? If it is CString, then as you can see in the documentation of CString::Find[^], you can specify it the starting character it will search from, so when n contains your first '\n', just feed find with n+1...
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <
-
I have a string object called str. The content within is "Line1Line1\nLine2Line2\n". I can find the positions of '\n's using char array and use them like this str[10], but how to do this with string object. What is the function for this? I mean I know about string.find() and use it like this: int n = 0; n = str.find("\n"); but what if I want the second '\n'?
modified on Thursday, October 21, 2010 3:28 PM
TCPMem wrote:
but what if I want the second '\n'?
Use the (incremented) last position as start for the next find():
std::string str = "Line1Line1\nLine2Line2\n";
for (size_t pos = 0; pos != str.npos; pos = str.find('\n', ++pos))
std::cout << str.substr(pos); // do sompething with poscheers, AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)