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. substr and find of string

substr and find of string

Scheduled Pinned Locked Moved C / C++ / MFC
regextutorialquestion
5 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.
  • G Offline
    G Offline
    George_George
    wrote on last edited by
    #1

    Hello everyone, Just two bacis questions about how to program in an elegant way. 1. About string.find, whether using string::npos or -1 to check no match find is more elegant? 2. About string.substr, whether checking return value is null or not is an elegant way to know if substr method is successful? thanks in advance, George

    K M 2 Replies Last reply
    0
    • G George_George

      Hello everyone, Just two bacis questions about how to program in an elegant way. 1. About string.find, whether using string::npos or -1 to check no match find is more elegant? 2. About string.substr, whether checking return value is null or not is an elegant way to know if substr method is successful? thanks in advance, George

      K Offline
      K Offline
      Karismatic
      wrote on last edited by
      #2

      1. About string.find, it would be elegant to use string::npos instead of -1. 2. About string.substr, Actually it return basic_string and this class has a function called empty which returns true if the string object contains no characters; false if it has at least one character. I guess this would be good practice.

      Regards, Pankaj Sachdeva There is no future lies in any job but future lies in the person who holds the job

      G 1 Reply Last reply
      0
      • G George_George

        Hello everyone, Just two bacis questions about how to program in an elegant way. 1. About string.find, whether using string::npos or -1 to check no match find is more elegant? 2. About string.substr, whether checking return value is null or not is an elegant way to know if substr method is successful? thanks in advance, George

        M Offline
        M Offline
        markkuk
        wrote on last edited by
        #3

        "Elegance" has nothing to do with your questions. You need a choice between several syntactically correct choices before you can start thinking about coding "elegance". 1. string::npos is correct, -1 is wrong. 2. string::substr throws an exception if it fails. It will never return NULL (assuming your STL implementation isn't broken).

        G 1 Reply Last reply
        0
        • K Karismatic

          1. About string.find, it would be elegant to use string::npos instead of -1. 2. About string.substr, Actually it return basic_string and this class has a function called empty which returns true if the string object contains no characters; false if it has at least one character. I guess this would be good practice.

          Regards, Pankaj Sachdeva There is no future lies in any job but future lies in the person who holds the job

          G Offline
          G Offline
          George_George
          wrote on last edited by
          #4

          Thanks Karismatic, 1.

          Karismatic wrote:

          1. About string.find, it would be elegant to use string::npos instead of -1.

          Why? More words please? 2. I have performed more experiments, is it more correct to handle out of range exception? I found if the parameter is not legal, out of range exception will be thrown. I am surprised why each possible exception from substr method is not documented in MSDN? regards, George

          1 Reply Last reply
          0
          • M markkuk

            "Elegance" has nothing to do with your questions. You need a choice between several syntactically correct choices before you can start thinking about coding "elegance". 1. string::npos is correct, -1 is wrong. 2. string::substr throws an exception if it fails. It will never return NULL (assuming your STL implementation isn't broken).

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #5

            Thanks markkuk, 1. Why -1 is wrong? From definition it should be the same as npos, any comments? 2. About the exception, I am surprised why each possible exception from substr method is not documented in MSDN? Where could we find what exceptions will be thrown in a specific STL method? regards, George

            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