【发布时间】:2020-05-31 15:02:58
【问题描述】:
假设我有这个字符串:
char *myTestString = "Hello AND test AND test2";
我想把它分解成集合 {Hello,test,test2},我终于可以对其进行迭代了。
或者我有
char *myTestString2 = "Hi AND there AND test AND test2";
我想将其分解为集合 {Hi,there,test,test2},稍后我可以对其进行迭代。
如何使用 C 实现这一点?
编辑:
另一个例子是拆分"Hello there AND test" 应该给出集合{Hello there,test}。
为了澄清"AND"在这里是分隔符。
【问题讨论】:
-
您正在寻找
strtok。 -
strtok不支持将另一个字符串作为分隔符,是吗? -
@Zeruno 忽略使用 strtok 的建议。他们是错误的建议。
-
这能回答你的问题吗? stackoverflow.com/questions/38675046/…
-
@Zeruno 看起来不仅仅是
"AND"是一个分隔符。"Hello AND test AND test2"中的空格没有出现在 ` { Hello, test, test2 }. How do you want txt like"Hello AND test"` 和"Hello (many spaces) AND (tabs, line-feeds, other white space) test"分解?
标签: c string split string-matching c-strings