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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to get substring from a string......

how to get substring from a string......

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionhelptutorial
8 Posts 4 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.
  • V Offline
    V Offline
    vijay victory
    wrote on last edited by
    #1

    Hi Experts, I m working on String.. and I wanted to copy a substring from a main string e.g:;-- String1 :- "Visual c++ is Great.."; subStr = "c++"; how can I get this??? I have used strstr() function to get substring.. but it returns first occurance and full sunstring follwing that,, plz help me.. I need it dedly.. I used another method to get it.. but its lengthy... thanx in advance... Victory,...

    J N 2 Replies Last reply
    0
    • V vijay victory

      Hi Experts, I m working on String.. and I wanted to copy a substring from a main string e.g:;-- String1 :- "Visual c++ is Great.."; subStr = "c++"; how can I get this??? I have used strstr() function to get substring.. but it returns first occurance and full sunstring follwing that,, plz help me.. I need it dedly.. I used another method to get it.. but its lengthy... thanx in advance... Victory,...

      J Offline
      J Offline
      Jijo Raj
      wrote on last edited by
      #2

      If you are using CString, then use CString::Mid()[^].

      // If you are using CString.
      CString csString = _T("Visual C++ is Great!!!");
      CString csSubStr = csString.Mid( 0, 10 );

      If you are using std::string then, you can use string::substr[^].

      // If you use std::string.
      std::string sString = _T("Visual C++ is Great!!!");
      std::string sSubStr = sString.substr( 0, 10 );

      Regards, Jijo.

      _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

      1 Reply Last reply
      0
      • V vijay victory

        Hi Experts, I m working on String.. and I wanted to copy a substring from a main string e.g:;-- String1 :- "Visual c++ is Great.."; subStr = "c++"; how can I get this??? I have used strstr() function to get substring.. but it returns first occurance and full sunstring follwing that,, plz help me.. I need it dedly.. I used another method to get it.. but its lengthy... thanx in advance... Victory,...

        N Offline
        N Offline
        Nibu babu thomas
        wrote on last edited by
        #3

        Member 3220373 wrote:

        and I wanted to copy a substring from a main string e.g:;-- String1 :- "Visual c++ is Great.."; subStr = "c++"; how can I get this??? I have used strstr() function to get substring.. but it returns first occurance and full sunstring follwing that,, plz help me.. I need it dedly.. I used another method to get it.. but its lengthy...

        Using CString... First use CString::Find and then CString::Mid to extract substring. Using std::string First use std::string::find and then std::string::substr.

        Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

        V 1 Reply Last reply
        0
        • N Nibu babu thomas

          Member 3220373 wrote:

          and I wanted to copy a substring from a main string e.g:;-- String1 :- "Visual c++ is Great.."; subStr = "c++"; how can I get this??? I have used strstr() function to get substring.. but it returns first occurance and full sunstring follwing that,, plz help me.. I need it dedly.. I used another method to get it.. but its lengthy...

          Using CString... First use CString::Find and then CString::Mid to extract substring. Using std::string First use std::string::find and then std::string::substr.

          Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

          V Offline
          V Offline
          vijay victory
          wrote on last edited by
          #4

          Thanx for ur reply.. I forget to tell u that.. I m not using MFC classes or function.s;;; Its just Win32 console application..... can i use CString in console application???? plz reply..

          N D 2 Replies Last reply
          0
          • V vijay victory

            Thanx for ur reply.. I forget to tell u that.. I m not using MFC classes or function.s;;; Its just Win32 console application..... can i use CString in console application???? plz reply..

            N Offline
            N Offline
            Nibu babu thomas
            wrote on last edited by
            #5

            Member 3220373 wrote:

            Its just Win32 console application..... can i use CString in console application????

            Quite easy to use MFC in console application. First link to MFC libraries and then call AfxWinInit and a global CWinApp instance, sample code...

            CWinApp app; // global
            // initialize MFC and print error on failure, inside main function
            if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
            {
            // TODO: change error code to suit your needs
            cerr << "Fatal Error: MFC initialization failed" << endl;
            return 1;
            }

            And if you are going to use MFC just for this purpose, I would discourage you from doing so, instead use std::string.

            Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

            V 1 Reply Last reply
            0
            • N Nibu babu thomas

              Member 3220373 wrote:

              Its just Win32 console application..... can i use CString in console application????

              Quite easy to use MFC in console application. First link to MFC libraries and then call AfxWinInit and a global CWinApp instance, sample code...

              CWinApp app; // global
              // initialize MFC and print error on failure, inside main function
              if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
              {
              // TODO: change error code to suit your needs
              cerr << "Fatal Error: MFC initialization failed" << endl;
              return 1;
              }

              And if you are going to use MFC just for this purpose, I would discourage you from doing so, instead use std::string.

              Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

              V Offline
              V Offline
              vijay victory
              wrote on last edited by
              #6

              Thanxxxxxxxxxxxxxxxxxxxxxx...a lot..... it works....

              N 1 Reply Last reply
              0
              • V vijay victory

                Thanxxxxxxxxxxxxxxxxxxxxxx...a lot..... it works....

                N Offline
                N Offline
                Nibu babu thomas
                wrote on last edited by
                #7

                Member 3220373 wrote:

                Thanxxxxxxxxxxxxxxxxxxxxxx...a lot.....

                Welcome!

                Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

                1 Reply Last reply
                0
                • V vijay victory

                  Thanx for ur reply.. I forget to tell u that.. I m not using MFC classes or function.s;;; Its just Win32 console application..... can i use CString in console application???? plz reply..

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  Member 3220373 wrote:

                  Its just Win32 console application.....

                  Then you can use strncpy() or memcpy().

                  "Love people and use things, not love things and use people." - Unknown

                  "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                  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