c++ - 如何在 C++ 中获取 a 字符串的一部分?

标签 c++

如何在 C++ 中获取字符串的一部分?我想知道从 0 到 i 的元素是什么。

最佳答案

您想使用 std::string::substr。这是一个例子,无耻地复制自 http://www.cplusplus.com/reference/string/string/substr/

// string::substr
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str="We think in generalities, but we live in details.";
                             // quoting Alfred N. Whitehead
  string str2, str3;
  size_t pos;

  str2 = str.substr (12,12); // "generalities"

  pos = str.find("live");    // position of "live" in str
  str3 = str.substr (pos);   // get from "live" to the end

  cout << str2 << ' ' << str3 << endl;

  return 0;
}

关于c++ - 如何在 C++ 中获取 a 字符串的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2498119/

相关文章:

c++ - "Destructor already defined"带有专门的析构函数

c++ - MFC C++ 将枚举值插入到 CArray

c++ - 将指针数组作为 const 传递

c++ - 期望以确切的对象实例作为参数调用

c++ - 是否可以将 Dword 转换为类型名称?

c++ - 如何使用函数 b( C 和 winapi )覆盖函数 a ?

c++ - 确定c++方法的大小

c++ - 来自 std::any 的 std::is_reference

java - C++ 调用 Java 的 JNI localrefs 的生命周期是多少?

c++ - 实现加权图