假设我有一段字符串 "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/