LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2007, 12:55 PM   #1
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Rep: Reputation: 30
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
 
Old 05-12-2007, 01:04 PM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
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.)
 
Old 05-12-2007, 03:00 PM   #3
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Original Poster
Rep: Reputation: 30
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.
 
Old 05-12-2007, 10:54 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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') {
 
Old 05-13-2007, 09:45 AM   #5
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Original Poster
Rep: Reputation: 30
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/

Last edited by bendeco13; 05-13-2007 at 09:47 AM.
 
Old 05-13-2007, 09:52 AM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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
 
Old 05-13-2007, 09:59 AM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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").
 
Old 05-13-2007, 10:06 AM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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;

Last edited by dmail; 05-13-2007 at 10:09 AM.
 
Old 05-13-2007, 10:24 AM   #9
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Original Poster
Rep: Reputation: 30
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!
 
Old 05-13-2007, 10:26 AM   #10
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Original Poster
Rep: Reputation: 30
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.
 
Old 05-13-2007, 10:28 AM   #11
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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

Last edited by dmail; 05-13-2007 at 10:29 AM.
 
Old 05-13-2007, 11:02 AM   #12
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Original Poster
Rep: Reputation: 30
Quote:
But do you think it will find "\n"?
It actually does!
 
  


Reply

Tags
c++



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl substr goes crazy when presented with zero's nodger Programming 4 01-21-2007 09:49 AM
perl: using 'substr' in regex ananthbv Programming 5 11-03-2004 01:58 AM
PHP (substr) & UTF-8 liquid sky Programming 3 09-08-2004 12:26 PM
C++: char* to string, converting? rewriting program? need substr! lrt2003 Programming 2 06-19-2004 03:20 AM
how to substr ? black Programming 5 12-23-2002 12:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration