How to extract part of a string in c++
To Extract part of a string in a given line:
Given line is:
D:\AB554P_Itr23\ModelDir\AB554P_approximation_NodeData.dat Read 3/18/2010
I need to extract only the file name AB554P_approximation_NodeData.dat
. I am using 开发者_开发技巧CodeGear RAD Studio C++ Builder. Thanks:)
Did you look into string::substr
How about Boost::Filesystem...
string name = path( "D:\\AB554P_Itr23\\ModelDir\\AB554P_approximation_NodeData.dat").filename()
The RTL function ExtractFileName()
will extract the file name from a path.
Exactly how to split the 3 fields in your example depends very much on what the rules are for forming that line of text. How is it delimited? Is is delimited by spaces? If so, how do you escape spaces in the file path? Until you specify that information, that part of your question is unanswerable.
In order to handle Windows paths, use the Windows API path functions, such as PathFindFileName
.
Cheers & hth.,
- Here C++ Parse Split Delimited String you get the main function.
- To delete part after space, use:
string s = "D:\AB554P_Itr23\ModelDir\AB554P_approximation_NodeData.dat Read 3/18/2010";
size_t pos;
pos = s.find(" ");
s.erase(pos);
Now call split
for '\' to split the remaining part.
You may want to investigate a regular expression library, such as Boost Regex.
精彩评论