-
ya
- ^-^
- Сообщения: 2336
- Зарегистрирован: 16 дек 2021, 19:56
Сообщение
ya »
Код: Выделить всё
#include <iostream>
#include <string>
int main() {
std::string path = "C:/my/folder/file.txt";
std::cout << "Original path: " << path << std::endl;
// Find and replace all slashes with backslashes
size_t pos = 0;
while ((pos = path.find('/', pos)) != std::string::npos) {
path.replace(pos, 1, "\\");
pos += 1;
}
std::cout << "Updated path: " << path << std::endl;
return 0;
}
-
ya
- ^-^
- Сообщения: 2336
- Зарегистрирован: 16 дек 2021, 19:56
Сообщение
ya »
Код: Выделить всё
std::string stringReplace(const std::string& path, const std::string& find_str, const std::string& replace_str)
{
// Find and replace
std::string modified_path = path; // Создадим изменённый путь, чтобы не менять оригинальный
size_t pos = 0;
while ((pos = modified_path.find(find_str, pos)) != std::string::npos) {
modified_path.replace(pos, find_str.length(), replace_str); // Заменяет правильно по длине find_str
pos += replace_str.length(); // Сдвинем позицию для дальнейших замен
}
return modified_path;
}