Страница 1 из 1
Замена подстроки строкой
Добавлено: 17 сен 2023, 14:24
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;
}
Re: Замена подстроки строкой
Добавлено: 12 сен 2024, 17:25
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;
}