在“-"上分割一个字符* [英] Splitting a char * over "->"

查看:62
本文介绍了在“-"上分割一个字符*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种在符号->"上拆分char *的方法.

范例:abc-> efg

必须分为"abc"和"efg"

尝试使用strtok,但问题是它认为->"成为两个不同的符号,并在以下情况下引起问题:
abc-efg-> hyj

理想情况下,拆分应导致两个字符串"abc-efg"和"hyj"
strtok给出三个"abc","efg"和"hyj"

任何建议

I need a way to split a char * over the symbol "->"

Example : abc->efg

has to be split to "abc" and "efg"

tried using strtok but the problem is that it considers "->" to be two different symbols and cause problems in cases like :
abc-efg->hyj

ideally the split should result in two strings "abc-efg" and "hyj"
strtok gives three "abc" "efg" and "hyj"

Any suggestions

推荐答案

这是因为strtok匹配任何字符作为分隔符,而不是所有字符: http://www.cplusplus.com/reference/clibrary/cstring/strstr/ [ ^ ]-您将必须反复调用它,但是它将执行您想要的操作.
That is because strtok matches any of the characters as delimiters, rather than all of them: http://www.cplusplus.com/reference/clibrary/cstring/strtok/[^]

Have a look at strstr instead: http://www.cplusplus.com/reference/clibrary/cstring/strstr/[^] - you will have to call it repeatedly, but it will do what you want.


这篇关于在“-"上分割一个字符*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆