Find characters in a string

This is a discussion on Find characters in a string within the C++ Programming forums, part of the General Programming Boards category; *edit* Nevermind, it looks like I just needed to add a backslash: Code: found = str.find(" \n "); ---------------------------- I'm ...

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    Find characters in a string

    *edit*
    Nevermind, it looks like I just needed to add a backslash:
    Code:
    found = str.find("\\n");
    ----------------------------
    I'm searching for a sequence of characters in a string. I figured .find() would be my best bet. I can get it to work for most cases, except when I try to look for the following sequence of characters "\n".

    So, my example string might look like:
    Code:
    The dog crossed \nthe street and he \nbarked at the neighbor.
    *Note, the "\n" is actually in the text file. So, the above string would look exactly as it does now - all on one line.

    I can find other sequences of characters no problem. For example:
    Code:
    int found = -999;
    
    found = str.find("do");
    cout <<  found << endl;
    This will return the correct position. However, if I do:
    Code:
    int found = -999;
    
    found = str.find("\n");
    cout <<  found << endl;
    It will return -1.

    I know it has something to do with '\n' representing newlines, but I can't quite figure out how to actually find "\n" in the string.

    In case you are wondering, I am just trying to replace any occurences of "\n" with an actual new line.
    So my output would look like:
    Code:
    The dog crossed
    the street and he
    barked at the neighbor.
    Maybe there is a better way to do this?

    Any ideas?
    Last edited by Cpro; 05-06-2011 at 02:36 PM.
    IDE - Visual Studio 2005
    Windows XP Pro

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Are you able to upgrade your IDE to 2010? If so, I'd recommend you do it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    19
    show your code maybe clear!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find index of last char in search string in string?
    By Programmer_P in forum C++ Programming
    Replies: 6
    Last Post: 06-07-2010, 07:51 PM
  2. Replies: 5
    Last Post: 05-09-2010, 11:58 AM
  3. find a string based on the location of another string
    By rlilley in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 12:29 PM
  4. how can I find type of characters?
    By arian in forum C# Programming
    Replies: 2
    Last Post: 09-20-2008, 06:28 PM
  5. Concatenating: characters->string->vector (string) :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2002, 01:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21