LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   substr() newline? (https://www.linuxquestions.org/questions/programming-9/substr-newline-553399/)

bendeco13 05-12-2007 12:55 PM

substr() newline?
 
I'm attempting to read a string and determine if it contains a newline, \n.

Code:

string txt = "line1\nline2";
bool hasline = false;
for (int a=0; a<strlen(txt_c_str()); a++) {
    if (txt.substr(a, 2) == "\n") {
        // This is whats not working since \n is read in as white space
        hasline = true;
        break;
    }
}

Any ideas on how I could accomplish this???
Thanks in advance,
Bendeco

indienick 05-12-2007 01:04 PM

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.)

bendeco13 05-12-2007 03:00 PM

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.

osor 05-12-2007 10:54 PM

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') {

bendeco13 05-13-2007 09:45 AM

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/

dmail 05-13-2007 09:52 AM

Quote:

Originally Posted by bendeco13
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/

http://www.cplusplus.com/reference/s...ring/find.html

osor 05-13-2007 09:59 AM

Quote:

Originally Posted by bendeco13
And thanks for the programming tip, but that doesn't help me find a solution.

Umm… that was a solution (i.e., use “if (txt.at(a) == '\n')”). The problem you’re encountering is because of the way in which you compare strings. The easiest thing to do is compare each character at a time, thus using “.at()”. You compare to a character rather than to a string by using the single quotes instead of the double quotes (this is why I’ve written '\n' instead of "\n").

dmail 05-13-2007 10:06 AM

Quote:

The easiest thing to do is compare each character at a time
Code:

std::string::size_type pos( foo.find('\n') );

if( pos == std::string::npos)std::cout <<"not found" <<std::endl;
else std::cout <<"found at least one at position " << pos <<std::endl;


bendeco13 05-13-2007 10:24 AM

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";
bool hasline = false;
int loc = txt.find("\n", 0);
if (a != string::npos) {
    hasline = true;
}

Thanks for all of the replies!

bendeco13 05-13-2007 10:26 AM

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.

dmail 05-13-2007 10:28 AM

Quote:

Originally Posted by bendeco13
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";
bool hasline = false;
int loc = txt.find("\n", 0);
if (a != string::npos) {
    hasline = true;
}

Thanks for all of the replies!

lmao thats nice to know:)
But do you think it will find "\n"?

hmm i cant delete the post :(

bendeco13 05-13-2007 11:02 AM

Quote:

But do you think it will find "\n"?
It actually does! :)


All times are GMT -5. The time now is 01:32 PM.