substr() newline?
I'm attempting to read a string and determine if it contains a newline, \n.
Code:
string txt = "line1\nline2"; Thanks in advance, Bendeco |
Can you search for the ASCII code for the NEWLINE character using a C function? Is there something that could allow you to do that in string.h?
(I'm assuming this is a C program - make sure when you start a new thread, if you don't specify the programming language in the thread title, at least do it in the OP.) |
Thanks for the reply,
I am coding in c++ and I will check into that. I figured there would be something along those lines, but I just didn't know where to look. |
First, the line “txt_c_str()” should be “txt.c_str()”. Second, why not use the “.at” member function of a string class. I.e.,
Code:
if (txt.at(a) == '\n') { |
yes, the txt_c_str() was a typo.
And thanks for the programming tip, but that doesn't help me find a solution. I've tried just about everything that I can think of and I have searched around for about 6 hours for a solution. I'm clueless. I've tried all of the following: if ((int)txt.substr(a, 2).c_str() == 10) { if ((int)txt[a] == 10) { if (txt.substr(a, 2).c_str() == "\012") { if (txt[a] == "\012"[0]) { Here's an ASCII table for reference. http://www.asciitable.com/ |
Quote:
|
Quote:
|
Quote:
Code:
std::string::size_type pos( foo.find('\n') ); |
I figured it out after stepping back and trying a whole different approach.
Here's what I ended up with. Code:
string txt = "line1\nline2"; |
Thanks dmail,
I was in the middle of typing out my solution. I didn't see your post until I reposted, but we both had the same approach. |
Quote:
But do you think it will find "\n"? hmm i cant delete the post :( |
Quote:
|
All times are GMT -5. The time now is 01:32 PM. |