索引字符串?

Tyl*_*ler 6 c++ string indexing

我是 C++ 新手,并且已经使用 python 一段时间了,但我知道 C++ 并不那么用户友好。我如何索引一个字符串,比如说 C++ 中 1:6 的“Example”?我只想索引内部元素而不是第一个或最后一个字符。

wjm*_*wjm 5

I want to index the inside elements.
Run Code Online (Sandbox Code Playgroud)

考虑以下程序:

#include <iostream>

int main()
{
    std::string str = "Example";

    if (str.length() > 2)
    {
        std::cout << str.substr(1, str.length() - 2);
    }
    else
    {
        std::cout << str;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它的输出是xampl.