-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_view.cpp
More file actions
39 lines (30 loc) · 1.05 KB
/
string_view.cpp
File metadata and controls
39 lines (30 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Extract extension function extracts and returns from a given filename
// the extension including the dot character
#include <string>
#include <string_view>
#include <print>
#include <filesystem>
// Extract extension (including the dot) from a filename
std::string extractExtension(std::string_view filename) {
// Find the last dot
std::size_t pos = filename.rfind('.');
if (pos == std::string_view::npos) {
return {}; // No extension found → return empty string
}
// Return a copy of the substring starting at the dot
return std::string{ filename.substr(pos) };
}
int main() {
std::string filename{
R"(/home/jose/Desktop/programming/cpp/filename.txt)"
};
std::println("C++ string: {}", extractExtension(filename));
std::string cString{
R"(/home/jose/Desktop/programming/cpp/file.txt)"
};
std::println("C string: {}", extractExtension(cString));
std::println("Literal: {}",
extractExtension(R"(/home/jose/Desktop/programming/cpp/filename2.txt)")
);
return 0;
}