substring in a std::string
-
can anyone tell me how to check whether a particular substring is present in a given string or not? I have a string containing file name and I have to check whether the file is an xml file or not. So I have to look for ".xml" substring in that given string. How do we do that?
-
can anyone tell me how to check whether a particular substring is present in a given string or not? I have a string containing file name and I have to check whether the file is an xml file or not. So I have to look for ".xml" substring in that given string. How do we do that?
Try something like this. NOTE: I haven't tested this so your mileage may vary: ---------------- string::size_type LastDotPos = YourString.find_last_of('.'); if ( LastDotPos != -1 ) { string Extension = YourString.substr(LastDotPos); transform(Extension.begin(), Extension.end(), Extension.begin(), ::tolower); if ( Extension == ".xml" ) { // Ends in ".xml". } } Steve