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. Extract string from CString

Extract string from CString

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
14 Posts 6 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.
  • A Anu_Bala

    I have to extract a few character from CString. My CString is like this... Here i have to get the number of Lbl..I mean 10. My code is CString sTemp = {"Tag-10,OnOff-19,Switch-5,Lbl-10,Signal-22,trip-1"}; int pos = sTemp.Find("Lbl-"); Here i get the pos value as 25,then i want to get the character after Lbl- (ie) 10. How can i get ?Pls help me.

    Anu

    R Offline
    R Offline
    Rajkumar R
    wrote on last edited by
    #3

    is this useful,

    CString sTemp = _T("Tag-10,OnOff-19,Switch-5,Lbl-10,Signal-22,trip-1");
    int pos = sTemp.Find(_T("Lbl")) + 3;
    CString resToken;
    resToken = sTemp.Tokenize(_T("- ,"),pos);
    if (resToken != "")
    {
    int num = _ttoi(resToken);
    };

    CPalliniC 1 Reply Last reply
    0
    • R Rajkumar R

      is this useful,

      CString sTemp = _T("Tag-10,OnOff-19,Switch-5,Lbl-10,Signal-22,trip-1");
      int pos = sTemp.Find(_T("Lbl")) + 3;
      CString resToken;
      resToken = sTemp.Tokenize(_T("- ,"),pos);
      if (resToken != "")
      {
      int num = _ttoi(resToken);
      };

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #4

      That goes against OP title. :-D

      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.
      [my articles]

      modified on Tuesday, February 05, 2008 8:41:55 AM

      In testa che avete, signor di Ceprano?

      R M 2 Replies Last reply
      0
      • CPalliniC CPallini

        That goes against OP title. :-D

        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.
        [my articles]

        modified on Tuesday, February 05, 2008 8:41:55 AM

        R Offline
        R Offline
        Rajkumar R
        wrote on last edited by
        #5

        may be more, but i think it covers what it needed:confused:

        1 Reply Last reply
        0
        • CPalliniC CPallini

          That goes against OP title. :-D

          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.
          [my articles]

          modified on Tuesday, February 05, 2008 8:41:55 AM

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #6

          What? No solutions that use GetBuffer()?? I am shocked and appalled. :)

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          CPalliniC 1 Reply Last reply
          0
          • M Mark Salsbery

            What? No solutions that use GetBuffer()?? I am shocked and appalled. :)

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #7

            Only for you, to keep horror alive

            strok(sTemp.GetBuffer(), "Lbl-");

            :-D

            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.
            [my articles]

            In testa che avete, signor di Ceprano?

            M 1 Reply Last reply
            0
            • CPalliniC CPallini

              Only for you, to keep horror alive

              strok(sTemp.GetBuffer(), "Lbl-");

              :-D

              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.
              [my articles]

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #8

              Ahhh yes - the world is balanced again. And bonus points for using the little-known strok() function hehe

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              1 Reply Last reply
              0
              • A Anu_Bala

                I have to extract a few character from CString. My CString is like this... Here i have to get the number of Lbl..I mean 10. My code is CString sTemp = {"Tag-10,OnOff-19,Switch-5,Lbl-10,Signal-22,trip-1"}; int pos = sTemp.Find("Lbl-"); Here i get the pos value as 25,then i want to get the character after Lbl- (ie) 10. How can i get ?Pls help me.

                Anu

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

                Use CString::Mid(), as NishantB++ already said to you.

                CString str = _T("Tag-10,OnOff-19,Switch-5,Lbl-10,Signal-22,trip-1");
                AfxMessageBox(str.Mid(str.Find(_T("Lbl-"))+4,2));//4 and 2 may vary! Depends on your string.

                Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                R 1 Reply Last reply
                0
                • R Rajesh R Subramanian

                  Use CString::Mid(), as NishantB++ already said to you.

                  CString str = _T("Tag-10,OnOff-19,Switch-5,Lbl-10,Signal-22,trip-1");
                  AfxMessageBox(str.Mid(str.Find(_T("Lbl-"))+4,2));//4 and 2 may vary! Depends on your string.

                  Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                  R Offline
                  R Offline
                  Rajkumar R
                  wrote on last edited by
                  #10

                  Rajesh R Subramanian wrote:

                  4 and 2 may vary!

                  I agree with 4 as he knows and is going to search using that string but if he knows 2 that means he already has the string with him and question is to find that string.

                  Anu_Bala wrote:

                  Here i have to get the number of Lbl..I mean 10.

                  R 1 Reply Last reply
                  0
                  • R Rajkumar R

                    Rajesh R Subramanian wrote:

                    4 and 2 may vary!

                    I agree with 4 as he knows and is going to search using that string but if he knows 2 that means he already has the string with him and question is to find that string.

                    Anu_Bala wrote:

                    Here i have to get the number of Lbl..I mean 10.

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

                    Apparently, you did not read the entire comment, did you? It's also mentioned "Depends on your string".

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                    R 1 Reply Last reply
                    0
                    • R Rajesh R Subramanian

                      Apparently, you did not read the entire comment, did you? It's also mentioned "Depends on your string".

                      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                      R Offline
                      R Offline
                      Rajkumar R
                      wrote on last edited by
                      #12

                      then the solution is if he want to find the number of Lbl, when it is 100, he want to build again :omg:

                      Rajesh R Subramanian wrote:

                      "Depends on your string".

                      Depends on the string, lbl10.exe, lbl110.exe ...

                      R 1 Reply Last reply
                      0
                      • R Rajkumar R

                        then the solution is if he want to find the number of Lbl, when it is 100, he want to build again :omg:

                        Rajesh R Subramanian wrote:

                        "Depends on your string".

                        Depends on the string, lbl10.exe, lbl110.exe ...

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

                        My mentioning "Depends on your string" states that he'd have to modify the code accordingly if the parameters change, so that it works always without having to rebuild. I thought it was pretty clear and anyone who fails to understand is just plainly dumb.

                        Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                        R 1 Reply Last reply
                        0
                        • R Rajesh R Subramanian

                          My mentioning "Depends on your string" states that he'd have to modify the code accordingly if the parameters change, so that it works always without having to rebuild. I thought it was pretty clear and anyone who fails to understand is just plainly dumb.

                          Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                          R Offline
                          R Offline
                          Rajkumar R
                          wrote on last edited by
                          #14

                          Rajesh R Subramanian wrote:

                          to modify the code accordingly if the parameters change

                          that is 2 to 3 when lbl is 100 that means he knows the length of string without the string i got. lbl10.exe no rebuild , lbl-100.exe no rebuild ... thanks :-D recalling ur mentioning of "Depends on your string"

                          Rajesh R Subramanian wrote:

                          //4 and 2 may vary! Depends on your string.

                          Rajesh R Subramanian wrote:

                          plainly dumb.

                          :omg: if he is such a person, i think he never post this, don't u even think, he is in need of this plain dumb solution of counting exercise, as he has NiShantB++ post already.

                          modified on Wednesday, February 06, 2008 11:30:57 PM

                          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