在 C++ 中切片字符串

问题描述 投票:0回答:1

我是编程新手,我从Python开始。我正在尝试通过删除第一个和最后一个字符来缩短 C++ 中的字符串。

在Python中,我使用了[]运算符。 C++ 中有等效的吗?

我现在没有任何线索。

c++ string
1个回答
0
投票

您无需实际更改字符串,只需从中创建一个

std::string_view

示例:

std::string const str = "~Hello World~";
std::string_view const view{std::next(str.begin()), std::prev(str.end())};

但请记住,如果绳子太短,它会在你脸上爆炸。

© www.soinside.com 2019 - 2024. All rights reserved.