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. TrimLeft() question

TrimLeft() question

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
6 Posts 3 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.
  • S Offline
    S Offline
    speedpacer
    wrote on last edited by
    #1

    I don't know what I'm doing wrong here but I'm getting some strange results from TrimLeft(). Take the following code for example:

    CString m_Anchorage = _T("C:\\Project\\Final\\Data\\Anchorage, AK"));
    CString m_Fairbanks = _T("C:\\Project\\Final\\Data\\Fairbanks, AK"));

    CString sAnchorage = m_Anchorage;
    CString sFairbanks = m_Fairbanks;

    sAnchorage.TrimLeft(_T("C:\\Project\\Final\\Data\\"));
    sFairbanks.TrimLeft(_T("C:\\Project\\Final\\Data\\"));

    MessageBox(sAnchorage,NULL,MB_OK);
    MessageBox(sFairbanks,NULL,MB_OK);

    The code doesn't really look like this, just showing it this way for simplicity's sake. In this case, "sAnchorage" will return properly, "Anchorage, AK". However, "sFairbanks" will return "rbanks, AK". As a matter of fact, anything that starts with the letter "F" is causing this. Anyone know why? :confused: Thanks in advance, Wolf

    S J 2 Replies Last reply
    0
    • S speedpacer

      I don't know what I'm doing wrong here but I'm getting some strange results from TrimLeft(). Take the following code for example:

      CString m_Anchorage = _T("C:\\Project\\Final\\Data\\Anchorage, AK"));
      CString m_Fairbanks = _T("C:\\Project\\Final\\Data\\Fairbanks, AK"));

      CString sAnchorage = m_Anchorage;
      CString sFairbanks = m_Fairbanks;

      sAnchorage.TrimLeft(_T("C:\\Project\\Final\\Data\\"));
      sFairbanks.TrimLeft(_T("C:\\Project\\Final\\Data\\"));

      MessageBox(sAnchorage,NULL,MB_OK);
      MessageBox(sFairbanks,NULL,MB_OK);

      The code doesn't really look like this, just showing it this way for simplicity's sake. In this case, "sAnchorage" will return properly, "Anchorage, AK". However, "sFairbanks" will return "rbanks, AK". As a matter of fact, anything that starts with the letter "F" is causing this. Anyone know why? :confused: Thanks in advance, Wolf

      S Offline
      S Offline
      speedpacer
      wrote on last edited by
      #2

      Sorry, nevermind. I get it. :sigh:

      S 1 Reply Last reply
      0
      • S speedpacer

        Sorry, nevermind. I get it. :sigh:

        S Offline
        S Offline
        speedpacer
        wrote on last edited by
        #3

        Still have a question though... I thought CString::Left() would work, but it's not working the way I expected either. So, how do I trim off the first part of this string, to get only the city and state? Better yet, how can I get the current directory, pass that to a CString, and then strip that value out of another string, as per the example above? Thanks, Wolf

        1 Reply Last reply
        0
        • S speedpacer

          I don't know what I'm doing wrong here but I'm getting some strange results from TrimLeft(). Take the following code for example:

          CString m_Anchorage = _T("C:\\Project\\Final\\Data\\Anchorage, AK"));
          CString m_Fairbanks = _T("C:\\Project\\Final\\Data\\Fairbanks, AK"));

          CString sAnchorage = m_Anchorage;
          CString sFairbanks = m_Fairbanks;

          sAnchorage.TrimLeft(_T("C:\\Project\\Final\\Data\\"));
          sFairbanks.TrimLeft(_T("C:\\Project\\Final\\Data\\"));

          MessageBox(sAnchorage,NULL,MB_OK);
          MessageBox(sFairbanks,NULL,MB_OK);

          The code doesn't really look like this, just showing it this way for simplicity's sake. In this case, "sAnchorage" will return properly, "Anchorage, AK". However, "sFairbanks" will return "rbanks, AK". As a matter of fact, anything that starts with the letter "F" is causing this. Anyone know why? :confused: Thanks in advance, Wolf

          J Offline
          J Offline
          John R Shaw
          wrote on last edited by
          #4

          I have been single stepping through this code and still have not found out what is happening. I have learned that if you substiture A in Anchorage with a printf white space formating character (n, t, f) then the same problem occurs. I recommend that you write your own TrimLeft() routine, it is easy to do. Good luck! Oh and thanks for the puzzler. I'll find a solution eventualy, but not today. INTP

          S 1 Reply Last reply
          0
          • J John R Shaw

            I have been single stepping through this code and still have not found out what is happening. I have learned that if you substiture A in Anchorage with a printf white space formating character (n, t, f) then the same problem occurs. I recommend that you write your own TrimLeft() routine, it is easy to do. Good luck! Oh and thanks for the puzzler. I'll find a solution eventualy, but not today. INTP

            S Offline
            S Offline
            speedpacer
            wrote on last edited by
            #5

            Thanks for your response. I've got it now. TrimLeft() (after a little RTFM'ing) removes ALL characters from the left that you specify, not the string as it appears. So, what I did was subtract the length of the current directory from the length of the entire string, and then use that number to extract only the data I wanted from the string, like so:

            CString m_Fairbanks = _T("C:\\Project\\Final\\Data\\Fairbanks, AK");
            int FairbanksLength = strlen(m_Fairbanks);
            TCHAR SourcePath[MAX_PATH+1] = {0};
            GetCurrentDirectory(MAX_PATH,SourcePath);
            int DirLength = strlen(SourcePath);
            int DataLength = FairbanksLength - DirLength;
            CString string = m_Fairbanks.Right(DataLength-1);
            MessageBox(string,NULL,MB_OK);

            I'm actually loading the data from an XML file and overriding an OnBeforeNavigate2() message from a WebBrowser Control, so it thinks it's a URL (VARIANT) and adding the local path to the element, which isn't what I wanted. It seems to be working now though. Thanks again, Wolf

            M 1 Reply Last reply
            0
            • S speedpacer

              Thanks for your response. I've got it now. TrimLeft() (after a little RTFM'ing) removes ALL characters from the left that you specify, not the string as it appears. So, what I did was subtract the length of the current directory from the length of the entire string, and then use that number to extract only the data I wanted from the string, like so:

              CString m_Fairbanks = _T("C:\\Project\\Final\\Data\\Fairbanks, AK");
              int FairbanksLength = strlen(m_Fairbanks);
              TCHAR SourcePath[MAX_PATH+1] = {0};
              GetCurrentDirectory(MAX_PATH,SourcePath);
              int DirLength = strlen(SourcePath);
              int DataLength = FairbanksLength - DirLength;
              CString string = m_Fairbanks.Right(DataLength-1);
              MessageBox(string,NULL,MB_OK);

              I'm actually loading the data from an XML file and overriding an OnBeforeNavigate2() message from a WebBrowser Control, so it thinks it's a URL (VARIANT) and adding the local path to the element, which isn't what I wanted. It seems to be working now though. Thanks again, Wolf

              M Offline
              M Offline
              Mike Dimmick
              wrote on last edited by
              #6

              I think PathStripPath will do what you want. The Path_Xxx_ APIs are very powerful.

              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