C++如何提取字符串的第一个和其余部分?

标签 c++ string

假设我有一段字符串 "abcd"我想要一个函数从 "abcd"获取 char 'a' 并且另一个从“abcd”获取“bcd”的函数。

最佳答案

假设您正在使用 std::string,您需要:

str[0] // for the first char
str.substr(1) // for the rest of string

如果您使用标准的 C char* 字符串,您需要:

str[0] // for the first char

((char*)str+1) // or
&str[1]        // for the rest of string

使用前一种方法时,请确保您正在捕获 out_of_range 异常,或者在调用它之前检查字符串长度。

关于C++如何提取字符串的第一个和其余部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6336955/

相关文章:

c++ - 如何在没有明确说明的情况下从 dll 中导出模板类?

c++ - 如何显示带有 opengl 纹理的灰度图像

c - 无论如何要检查 str 函数是否成功?

python - 将 pandas Dataframe 行写入文本文件

python - 在 for 循环中使用 str.replace 将非字母数字字符替换为星号

c++ - glReadPixels 从错误的位置读取

c++ - 进行大乘法运算时避免 int 溢出

c++ - gcc 4.6 出现奇怪的模板错误消息

python - 使用用户数据编辑 URL Python Mechanize

ios - 如何摆脱 Swift 字符串中的 "non-breaking space"?