Splitting a CString
-
Is there a way to split a CString as you can in Perl? Need to find a way to take a string and split it based on a delemiter. for example if I have a string that looks like c:\data\moredata\evenmoredata\datafile.dat I need to be able to split "datafile" from the string. Since the open file Dialog supports multiple selections I can't use .GetFileTitle(); Any ideas?
-
Is there a way to split a CString as you can in Perl? Need to find a way to take a string and split it based on a delemiter. for example if I have a string that looks like c:\data\moredata\evenmoredata\datafile.dat I need to be able to split "datafile" from the string. Since the open file Dialog supports multiple selections I can't use .GetFileTitle(); Any ideas?
-
Is there a way to split a CString as you can in Perl? Need to find a way to take a string and split it based on a delemiter. for example if I have a string that looks like c:\data\moredata\evenmoredata\datafile.dat I need to be able to split "datafile" from the string. Since the open file Dialog supports multiple selections I can't use .GetFileTitle(); Any ideas?
Hello there, Use CString's ReverseFind(..) function to get the position of the first backslash starting the search from the end of the string then use the Right(..) function to extract the datafile.dat name. If you don't want the extenstion you could use the Reverse find funcion again on the initial string and use the Mid(..) function to extract the name between the last backslash and the period. Alternatively you could have called CString's GetBuffer(..) on the string and tokenize the result it using strtok() or some other low lever function. I would advise the first approach. Art
-
Hello there, Use CString's ReverseFind(..) function to get the position of the first backslash starting the search from the end of the string then use the Right(..) function to extract the datafile.dat name. If you don't want the extenstion you could use the Reverse find funcion again on the initial string and use the Mid(..) function to extract the name between the last backslash and the period. Alternatively you could have called CString's GetBuffer(..) on the string and tokenize the result it using strtok() or some other low lever function. I would advise the first approach. Art
I Looked over MSDN information on both ReverseFind() and strtok() before you posted this information. I did not really understand the ReverseFind() function when I first read the information, and ended up implementing strtok() to do the work, however the code that I am using to do it looks rather nasty.. but it works.. however I will look more into ReverseFind() and see if I can implement this for hopefully cleaner code. Thank you very much for the information.
-
I Looked over MSDN information on both ReverseFind() and strtok() before you posted this information. I did not really understand the ReverseFind() function when I first read the information, and ended up implementing strtok() to do the work, however the code that I am using to do it looks rather nasty.. but it works.. however I will look more into ReverseFind() and see if I can implement this for hopefully cleaner code. Thank you very much for the information.
Here you are: CString sPath(_T("c:\\data\\moredata\\evenmoredata\\datafile.dat")); int nPos1 = sPath.ReverseFind('\\'); int nPos2 = sPath.ReverseFind('.'); if(nPos1 > -1 && nPos2 > -1) { CString sFile = sPath.Mid(nPos1+1, nPos2 - nPos1 -1); AfxMessageBox(sFile); } ----------------- The ReverseFind fn just searches the string starting from the end toward the beginning and stops when it encounters the character it is looking for. The position it reports is referenced from the beginning of the string. Art