ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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]) {
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]) {
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").
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;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.