TrimLeft() question
-
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
-
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
Sorry, nevermind. I get it. :sigh:
-
Sorry, nevermind. I get it. :sigh:
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
-
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
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
-
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
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
-
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
I think
PathStripPath
will do what you want. ThePath_Xxx_
APIs are very powerful.