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. Substring from a string

Substring from a string

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 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.
  • C Offline
    C Offline
    CodingLover
    wrote on last edited by
    #1

    Hi all, I have string like this. First name: name I want to get the string after the ':'. What is the easiest way. I'm wired with my way, find the length, find the place where : sign include and stuck...

    I appreciate your help all the time... Eranga :)

    R D C 3 Replies Last reply
    0
    • C CodingLover

      Hi all, I have string like this. First name: name I want to get the string after the ':'. What is the easiest way. I'm wired with my way, find the length, find the place where : sign include and stuck...

      I appreciate your help all the time... Eranga :)

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      If you're using MFC, it'd be easily done with the CString::Mid() function:

      CString str = \_T("Name: Rajesh");
      int iPos = str.Find(\_T(":"),0);
      AfxMessageBox(str.Mid(iPos+1));
      

      Or, if you aren't using MFC, then perhaps this helps you:

      TCHAR \*tch = new TCHAR(20);
      tch = \_T("Name:Rajesh");
      
      while(tch++)
      {
      	if(\*tch == ':')
      	{
      		::MessageBox(NULL, (tch+1), \_T("Message"), MB\_ICONINFORMATION);
      		break;
      	}
      }delete tch;
      

      Last modified: 15mins after originally posted --

      Rajesh.

      C 1 Reply Last reply
      0
      • R Rajesh R Subramanian

        If you're using MFC, it'd be easily done with the CString::Mid() function:

        CString str = \_T("Name: Rajesh");
        int iPos = str.Find(\_T(":"),0);
        AfxMessageBox(str.Mid(iPos+1));
        

        Or, if you aren't using MFC, then perhaps this helps you:

        TCHAR \*tch = new TCHAR(20);
        tch = \_T("Name:Rajesh");
        
        while(tch++)
        {
        	if(\*tch == ':')
        	{
        		::MessageBox(NULL, (tch+1), \_T("Message"), MB\_ICONINFORMATION);
        		break;
        	}
        }delete tch;
        

        Last modified: 15mins after originally posted --

        Rajesh.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        And what is the purpose of heap allocated memory?

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        R 1 Reply Last reply
        0
        • C CPallini

          And what is the purpose of heap allocated memory?

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          My bad mood perhaps? Seriously, just typed it out fast, tried and pasted here. Shouldn't have been fragmenting the precious heap though. Will take care of this in the future, my lord. :)

          C 1 Reply Last reply
          0
          • C CodingLover

            Hi all, I have string like this. First name: name I want to get the string after the ':'. What is the easiest way. I'm wired with my way, find the length, find the place where : sign include and stuck...

            I appreciate your help all the time... Eranga :)

            D Offline
            D Offline
            Don Box
            wrote on last edited by
            #5

            If CString is used, write

            CString str;
            str.Mid (str.Find (':'));

            If not using CString, write

            char* pszName = strstr (variable, ":");
            if (pszName != NULL)
            {
            pszName is valid pointer.
            }

            Come online at:- jubinc@skype

            C 1 Reply Last reply
            0
            • R Rajesh R Subramanian

              My bad mood perhaps? Seriously, just typed it out fast, tried and pasted here. Shouldn't have been fragmenting the precious heap though. Will take care of this in the future, my lord. :)

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              Rajesh R Subramanian wrote:

              my lord

              Wasn't Dunn? :laugh:

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              R 1 Reply Last reply
              0
              • C CPallini

                Rajesh R Subramanian wrote:

                my lord

                Wasn't Dunn? :laugh:

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                CPallini wrote:

                Wasn't Dunn?

                Opph korse he izz. :laugh:

                C 1 Reply Last reply
                0
                • R Rajesh R Subramanian

                  CPallini wrote:

                  Wasn't Dunn?

                  Opph korse he izz. :laugh:

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  :laugh:

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                  1 Reply Last reply
                  0
                  • C CodingLover

                    Hi all, I have string like this. First name: name I want to get the string after the ':'. What is the easiest way. I'm wired with my way, find the length, find the place where : sign include and stuck...

                    I appreciate your help all the time... Eranga :)

                    C Offline
                    C Offline
                    CodingLover
                    wrote on last edited by
                    #9

                    This what I have done

                        string strPT("First name: name");
                    int strLength = strPT.length();
                    int indexOfCol = strPT.find(':');
                    
                    string strMess = strPT.substr(indexOfCol + 2, (strLength - indexOfCol));
                    

                    I appreciate your help all the time... Eranga :)

                    1 Reply Last reply
                    0
                    • D Don Box

                      If CString is used, write

                      CString str;
                      str.Mid (str.Find (':'));

                      If not using CString, write

                      char* pszName = strstr (variable, ":");
                      if (pszName != NULL)
                      {
                      pszName is valid pointer.
                      }

                      Come online at:- jubinc@skype

                      C Offline
                      C Offline
                      CodingLover
                      wrote on last edited by
                      #10

                      Thanks, actually I'm trying this on standard C++ pal :)

                      I appreciate your help all the time... Eranga :)

                      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