这是我的代码:
char *reason = strtok(NULL, "\n");
std::string kickreason = "No reason";
if(reason)
kickreason = reason;
如何让“kickreason”只读 1 个字?所以如果用户输入“haha lol XD”它只会读到“haha”?
这是我的代码:
char *reason = strtok(NULL, "\n");
std::string kickreason = "No reason";
if(reason)
kickreason = reason;
如何让“kickreason”只读 1 个字?所以如果用户输入“haha lol XD”它只会读到“haha”?
将字符串放入 astd::istringstream
并使用普通输入运算符>>
:
std::istringstream iss("haha lol XD");
std::string word;
iss >> word;
std::cout << "First word is \"" << word << "\"\n";
stringstream::operator>>(string)
? 那是您需要查看的地方。
像这样:
istringstream str("no reason");
string oneword = "";
str >> oneword;
如何使用 strtok()
#include <stdio.h>
#include <iostream>
int main(int argc, char *argv[]) {
char sentence[] = "haha lol XD";
char * word;
std::cout << "Your sentence: " << sentence << std::endl;
word = strtok (sentence," ");
std::cout << "First word: " << word << std::endl;
return 0;
}
输出是
Your sentence: haha lol XD
First word: haha