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. String Problem

String Problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
41 Posts 13 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 rdop

    Hi All I have a problem to cut the string.How can i cut this string last file name. Old String

    C:\abc\cd\as.txt

    And i want to show New String

    C:\abc\cd

    Plz help me

    H Offline
    H Offline
    Hamid Taebi
    wrote on last edited by
    #2

    You can use of CStringMid or CString:Left.

    1 Reply Last reply
    0
    • R rdop

      Hi All I have a problem to cut the string.How can i cut this string last file name. Old String

      C:\abc\cd\as.txt

      And i want to show New String

      C:\abc\cd

      Plz help me

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #3

      You can use CString::ReverseFind() to get the zero based index of the last backslash and then truncate it with CString::Mid(). You can also use _splitpath() to split a path into its contents and then rebuild the string.

      "It's supposed to be hard, otherwise anybody could do it!" - selfquote
      "High speed never compensates for wrong direction!" - unknown

      R 1 Reply Last reply
      0
      • R rdop

        Hi All I have a problem to cut the string.How can i cut this string last file name. Old String

        C:\abc\cd\as.txt

        And i want to show New String

        C:\abc\cd

        Plz help me

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

        How is declared your string? :)

        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]

        In testa che avete, signor di Ceprano?

        R 1 Reply Last reply
        0
        • CPalliniC CPallini

          How is declared your string? :)

          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
          rdop
          wrote on last edited by
          #5

          This type

          CString array[10]

          CPalliniC D 2 Replies Last reply
          0
          • R rdop

            This type

            CString array[10]

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

            Hence follow the Roger Stoltz's advice [^].

            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]

            In testa che avete, signor di Ceprano?

            R 1 Reply Last reply
            0
            • R Roger Stoltz

              You can use CString::ReverseFind() to get the zero based index of the last backslash and then truncate it with CString::Mid(). You can also use _splitpath() to split a path into its contents and then rebuild the string.

              "It's supposed to be hard, otherwise anybody could do it!" - selfquote
              "High speed never compensates for wrong direction!" - unknown

              R Offline
              R Offline
              rdop
              wrote on last edited by
              #7

              Can you give me example.Plz help me

              R 1 Reply Last reply
              0
              • R rdop

                Can you give me example.Plz help me

                R Offline
                R Offline
                Roger Stoltz
                wrote on last edited by
                #8

                Something like the snippet below This is at the top of my head and I haven't tried to compile it, but you should get the idea anyway...

                CString FullPath = _T("C:\\abc\\cd\\as.txt");
                CString DirPath;
                int nPos = FullPath.ReverseFind( _T('\\') );

                if( nPos >= 0 )
                {
                DirPath = FullPath.Mid( 0, nPos + 1 );
                }

                "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                "High speed never compensates for wrong direction!" - unknown

                1 Reply Last reply
                0
                • CPalliniC CPallini

                  Hence follow the Roger Stoltz's advice [^].

                  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
                  rdop
                  wrote on last edited by
                  #9

                  Sir My Problem is that i want to cut File name in the exiting string.

                  C S 2 Replies Last reply
                  0
                  • R rdop

                    Sir My Problem is that i want to cut File name in the exiting string.

                    C Offline
                    C Offline
                    Chandrasekharan P
                    wrote on last edited by
                    #10

                    u can use the ReverseFind() and mid() functions to cut the file name. please check the examples the functions.

                    1 Reply Last reply
                    0
                    • R rdop

                      Sir My Problem is that i want to cut File name in the exiting string.

                      S Offline
                      S Offline
                      santhoshv84
                      wrote on last edited by
                      #11

                      Hi, Roget scoltz has given a nice solution. Did you tried that. CString FullPath = _T("C:\\abc\\cd\\as.txt"); CString DirPath; int nPos = FullPath.ReverseFind( _T('\\') ); if( nPos >= 0 ) { DirPath = FullPath.Mid( 0, nPos); } AfxMessageBox(DirPath);

                      R 1 Reply Last reply
                      0
                      • S santhoshv84

                        Hi, Roget scoltz has given a nice solution. Did you tried that. CString FullPath = _T("C:\\abc\\cd\\as.txt"); CString DirPath; int nPos = FullPath.ReverseFind( _T('\\') ); if( nPos >= 0 ) { DirPath = FullPath.Mid( 0, nPos); } AfxMessageBox(DirPath);

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

                        Thank it is working. Can i cut like this C:\\abc.If yes then plz help me

                        R 1 Reply Last reply
                        0
                        • R rdop

                          Thank it is working. Can i cut like this C:\\abc.If yes then plz help me

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

                          Roger said to you how to do it, you then comeback to ask for code. Now, there's something else which is utterly similar, but you want someone else to do it for you. Why don't you write your code yourself? Today is a good day to start.

                          Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

                          R 1 Reply Last reply
                          0
                          • R Rajesh R Subramanian

                            Roger said to you how to do it, you then comeback to ask for code. Now, there's something else which is utterly similar, but you want someone else to do it for you. Why don't you write your code yourself? Today is a good day to start.

                            Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

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

                            Not code just tell me how can i cut this C:\\abc\\cd\\as.txt and show C:\abc. Thanks in advance

                            R 1 Reply Last reply
                            0
                            • R rdop

                              Not code just tell me how can i cut this C:\\abc\\cd\\as.txt and show C:\abc. Thanks in advance

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

                              The idea was given to you here[^]. Why don't you try to devise a logic? If you want to strip down to the first directory from the root, then why not use CString::Find() instead of CString::ReverseFind() ?!

                              Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

                              M R 2 Replies Last reply
                              0
                              • R Rajesh R Subramanian

                                The idea was given to you here[^]. Why don't you try to devise a logic? If you want to strip down to the first directory from the root, then why not use CString::Find() instead of CString::ReverseFind() ?!

                                Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

                                M Offline
                                M Offline
                                Michael Schubert
                                wrote on last edited by
                                #16

                                You're wasting your time, Rajesh. He just wants a quick fix, he's too lazy to attempt to understand what he's doing or what he should be doing.

                                R 1 Reply Last reply
                                0
                                • R Rajesh R Subramanian

                                  The idea was given to you here[^]. Why don't you try to devise a logic? If you want to strip down to the first directory from the root, then why not use CString::Find() instead of CString::ReverseFind() ?!

                                  Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

                                  R Offline
                                  R Offline
                                  rdop
                                  wrote on last edited by
                                  #17

                                  I am useing this.

                                  CString DirPath;
                                  CString FullPath = _T("C:\\abc\\cd\\as.txt");

                                  int nPos = FullPath.Find( \_T('\\\\') );
                                  
                                  if( nPos >= 0 )
                                  {
                                  DirPath = FullPath.Mid( 0, nPos);
                                  }
                                  AfxMessageBox(DirPath);
                                  

                                  Then i am geting out put C: But i need to C:\abc. Plz tell me what i use. Thanks in advance

                                  R CPalliniC S 3 Replies Last reply
                                  0
                                  • R rdop

                                    I am useing this.

                                    CString DirPath;
                                    CString FullPath = _T("C:\\abc\\cd\\as.txt");

                                    int nPos = FullPath.Find( \_T('\\\\') );
                                    
                                    if( nPos >= 0 )
                                    {
                                    DirPath = FullPath.Mid( 0, nPos);
                                    }
                                    AfxMessageBox(DirPath);
                                    

                                    Then i am geting out put C: But i need to C:\abc. Plz tell me what i use. Thanks in advance

                                    R Offline
                                    R Offline
                                    Roger Stoltz
                                    wrote on last edited by
                                    #18

                                    rdop wrote:

                                    Then i am geting out put C: But i need to C:\abc. Plz tell me what i use.

                                    I'm terribly sorry, but this is logic on kindergarten level. If you sincerely do not understand, or you're too lazy to figure out why you got that result and what to do to fix it, I suggest you find another profession. Best of luck. :rose: -- Roger

                                    "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                                    "High speed never compensates for wrong direction!" - unknown

                                    R 1 Reply Last reply
                                    0
                                    • R Roger Stoltz

                                      rdop wrote:

                                      Then i am geting out put C: But i need to C:\abc. Plz tell me what i use.

                                      I'm terribly sorry, but this is logic on kindergarten level. If you sincerely do not understand, or you're too lazy to figure out why you got that result and what to do to fix it, I suggest you find another profession. Best of luck. :rose: -- Roger

                                      "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                                      "High speed never compensates for wrong direction!" - unknown

                                      R Offline
                                      R Offline
                                      rdop
                                      wrote on last edited by
                                      #19

                                      Ok i got your point what i will do.I will try to move other field.Can you give me solution of that problem. Thanks in advance

                                      CPalliniC 1 Reply Last reply
                                      0
                                      • R rdop

                                        I am useing this.

                                        CString DirPath;
                                        CString FullPath = _T("C:\\abc\\cd\\as.txt");

                                        int nPos = FullPath.Find( \_T('\\\\') );
                                        
                                        if( nPos >= 0 )
                                        {
                                        DirPath = FullPath.Mid( 0, nPos);
                                        }
                                        AfxMessageBox(DirPath);
                                        

                                        Then i am geting out put C: But i need to C:\abc. Plz tell me what i use. Thanks in advance

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

                                        rdop wrote:

                                        int nPos = FullPath.Find( _T('\\') );

                                        Why do you use Find, instead of (suggested) ReverseFind? :) [added] BTW since now the request is urgent, please feel free to visit www.cpallini.freeproductz.com to get the JITS (Just In Time Solution). :-D [/added]

                                        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]

                                        modified on Friday, October 3, 2008 7:45 AM

                                        In testa che avete, signor di Ceprano?

                                        R S I 3 Replies Last reply
                                        0
                                        • M Michael Schubert

                                          You're wasting your time, Rajesh. He just wants a quick fix, he's too lazy to attempt to understand what he's doing or what he should be doing.

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

                                          Michael Schubert wrote:

                                          He just wants a quick fix

                                          Ah, I hadn't realized it was urgnz. :)

                                          Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

                                          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