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. CString

CString

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 5 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.
  • R Offline
    R Offline
    raju_shiva
    wrote on last edited by
    #1

    Hi all, I am trying to trim a string and store the values in some variable. i.e I have

    CString str = sample(1,2,3,4,5);

    I want to store the values as
    x1=1,x2=2,x3=3......so on.

    Any idea how can i proceed will be thankful Thanks in advance Raj

    N S C 3 Replies Last reply
    0
    • R raju_shiva

      Hi all, I am trying to trim a string and store the values in some variable. i.e I have

      CString str = sample(1,2,3,4,5);

      I want to store the values as
      x1=1,x2=2,x3=3......so on.

      Any idea how can i proceed will be thankful Thanks in advance Raj

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #2

      I assume you mean

      CString str = sample("1,2,3,4,5");

      You can use std::stringstream, ::strtok(), CString::Find(), or maybe boost::split(). Try each of them and see how they differ.

      home

      1 Reply Last reply
      0
      • R raju_shiva

        Hi all, I am trying to trim a string and store the values in some variable. i.e I have

        CString str = sample(1,2,3,4,5);

        I want to store the values as
        x1=1,x2=2,x3=3......so on.

        Any idea how can i proceed will be thankful Thanks in advance Raj

        S Offline
        S Offline
        Shivanand Gupta
        wrote on last edited by
        #3

        Dear, Firstly assign object of CString correctly.

        modified on Wednesday, August 25, 2010 2:44 AM

        G 1 Reply Last reply
        0
        • R raju_shiva

          Hi all, I am trying to trim a string and store the values in some variable. i.e I have

          CString str = sample(1,2,3,4,5);

          I want to store the values as
          x1=1,x2=2,x3=3......so on.

          Any idea how can i proceed will be thankful Thanks in advance Raj

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

          raju_shiva wrote:

          CString str = sample(1,2,3,4,5);

          The above instruction is not correct. Supposing it is:

          CString str = "sample(1,2,3,4,5)"

          I would use CStringT::Tokenize [^], have a look at the sample code provided. :)

          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
          [My articles]

          R 1 Reply Last reply
          0
          • C CPallini

            raju_shiva wrote:

            CString str = sample(1,2,3,4,5);

            The above instruction is not correct. Supposing it is:

            CString str = "sample(1,2,3,4,5)"

            I would use CStringT::Tokenize [^], have a look at the sample code provided. :)

            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
            [My articles]

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

            CPallini wrote:

            Supposing it is: CString str = "sample(1,2,3,4,5)"

            Actually i am reading it from edit box as a CString. i.e CString str = "sample(1,2,3,4,5)" I am trying with this code

            CString str = "sample(1,2,3,4,5)",str1,str2;

            str1 = str .Left(str .Find("("));
            str2 = str .Right(str .Find("("));

            str.Left is working fine I am getting the result as sample But str.Right i am not getting the required result as 1,2,3,4,5). I am getting 3,4,5 What i am doing wrong Thanks Raj

            C S 2 Replies Last reply
            0
            • R raju_shiva

              CPallini wrote:

              Supposing it is: CString str = "sample(1,2,3,4,5)"

              Actually i am reading it from edit box as a CString. i.e CString str = "sample(1,2,3,4,5)" I am trying with this code

              CString str = "sample(1,2,3,4,5)",str1,str2;

              str1 = str .Left(str .Find("("));
              str2 = str .Right(str .Find("("));

              str.Left is working fine I am getting the result as sample But str.Right i am not getting the required result as 1,2,3,4,5). I am getting 3,4,5 What i am doing wrong Thanks Raj

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

              raju_shiva wrote:

              str1 = str .Left(str .Find("(")); str2 = str .Right(str .Find("("));

              Change to:

              int pos = str.Find("(");
              str1 = str.Left(pos);
              str2 = str.Right( str.GetLength() - pos - 1);

              However, I still would use Tokenize for the overall task. :)

              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
              [My articles]

              R 1 Reply Last reply
              0
              • S Shivanand Gupta

                Dear, Firstly assign object of CString correctly.

                modified on Wednesday, August 25, 2010 2:44 AM

                G Offline
                G Offline
                goorley
                wrote on last edited by
                #7

                What's wrong with assigning a variable from a function call?

                C 1 Reply Last reply
                0
                • G goorley

                  What's wrong with assigning a variable from a function call?

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

                  No problem, that's

                  CString sample(int a, int b, int c, int d, int e)
                  {
                  CString str;
                  str.Format("%d,%d,%d,%d,%d", a,b,c,d,e);
                  return str;
                  }

                  CString str = sample(1,2,3,4,5);
                  // now extract the numbers from the string!

                  just pure humour!

                  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
                  [My articles]

                  1 Reply Last reply
                  0
                  • R raju_shiva

                    CPallini wrote:

                    Supposing it is: CString str = "sample(1,2,3,4,5)"

                    Actually i am reading it from edit box as a CString. i.e CString str = "sample(1,2,3,4,5)" I am trying with this code

                    CString str = "sample(1,2,3,4,5)",str1,str2;

                    str1 = str .Left(str .Find("("));
                    str2 = str .Right(str .Find("("));

                    str.Left is working fine I am getting the result as sample But str.Right i am not getting the required result as 1,2,3,4,5). I am getting 3,4,5 What i am doing wrong Thanks Raj

                    S Offline
                    S Offline
                    Shivanand Gupta
                    wrote on last edited by
                    #9

                    I am giving example as your requirement. eg. CString str = "sample(1,2,3,4,5)",str1,str2; int curPos= 0; str1 = str.Tokenize("()",curPos); Output: str1 = 1,2,3,4,5

                    1 Reply Last reply
                    0
                    • C CPallini

                      raju_shiva wrote:

                      str1 = str .Left(str .Find("(")); str2 = str .Right(str .Find("("));

                      Change to:

                      int pos = str.Find("(");
                      str1 = str.Left(pos);
                      str2 = str.Right( str.GetLength() - pos - 1);

                      However, I still would use Tokenize for the overall task. :)

                      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
                      [My articles]

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

                      Hi, Thank you for your reply. I have used for() loop and find to split the string. As i am not familar with Tokenize,thats why i did not used it. Thanks Raj

                      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