Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to find the positions of '\n' within a string object [modified]

How to find the positions of '\n' within a string object [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structurestutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sadas232341s
    wrote on last edited by
    #1

    I have a string object called str. The content within is "Line1Line1\nLine2Line2\n". I can find the positions of '\n's using char array and use them like this str[10], but how to do this with string object. What is the function for this? I mean I know about string.find() and use it like this: int n = 0; n = str.find("\n"); but what if I want the second '\n'?

    modified on Thursday, October 21, 2010 3:28 PM

    C A 2 Replies Last reply
    0
    • S sadas232341s

      I have a string object called str. The content within is "Line1Line1\nLine2Line2\n". I can find the positions of '\n's using char array and use them like this str[10], but how to do this with string object. What is the function for this? I mean I know about string.find() and use it like this: int n = 0; n = str.find("\n"); but what if I want the second '\n'?

      modified on Thursday, October 21, 2010 3:28 PM

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      What is "string object"? CString? std::string? If it is CString, then as you can see in the documentation of CString::Find[^], you can specify it the starting character it will search from, so when n contains your first '\n', just feed find with n+1...

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

      1 Reply Last reply
      0
      • S sadas232341s

        I have a string object called str. The content within is "Line1Line1\nLine2Line2\n". I can find the positions of '\n's using char array and use them like this str[10], but how to do this with string object. What is the function for this? I mean I know about string.find() and use it like this: int n = 0; n = str.find("\n"); but what if I want the second '\n'?

        modified on Thursday, October 21, 2010 3:28 PM

        A Offline
        A Offline
        Alain Rist
        wrote on last edited by
        #3

        TCPMem wrote:

        but what if I want the second '\n'?

        Use the (incremented) last position as start for the next find():

        std::string str = "Line1Line1\nLine2Line2\n";
        for (size_t pos = 0; pos != str.npos; pos = str.find('\n', ++pos))
        std::cout << str.substr(pos); // do sompething with pos

        cheers, AR

        When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups