0

I'm using strtok in the hope of doing something like Php's explode.

At first I thought the following was working correctly

details = strtok(line,"]:");

But after some closer inspection I realised it was using any instance of ] and : to split the string. What I need is for it to only split the string where the two are together - ]:.

Maybe strtok is the wrong function for this? I played around with str_split but this didn't work, either through my implementation or unsuitability.

Any help welcome on splitting my string where an occurrence of ]: is found.

develophper
  • 302
  • 1
  • 3
  • 18
  • Check out my function to [split at any string](http://stackoverflow.com/a/22832368/2564301). – Jongware Sep 07 '15 at 21:29
  • @user3121023 strstr() is not sufficient - he wants to split the entire string, not just find the first occurrence. – jarmod Sep 07 '15 at 21:38

2 Answers2

1

strtok is the wrong function to use as it will split on any of the characters in the set of delimiters.

I'm not aware of any standard function to do what you want. You might have to roll your own.

Community
  • 1
  • 1
jarmod
  • 71,565
  • 16
  • 115
  • 122
  • thanks for the answer. I will check out that link. Btw I didn't downvote you. – develophper Sep 07 '15 at 21:25
  • @ThomasRyan Yep, downvoters should be required to provide a reason, though I'm OK that they remain anonymous. They may have a good point to make, even when it's not clear to others. – jarmod Sep 07 '15 at 21:32
  • @jarmod not my downvote, but fyi the reason is in the button's tooltip: `this answer is not useful`. Person A might find an answer useful while person B might not – Tim Sep 07 '15 at 21:39
  • @TimCastelijns Thanks for the info, I had not noticed that. – jarmod Sep 07 '15 at 22:13
1

In C "splitting" a string is really a matter of inserting a null terminator (0x00) in the string itself and let a new char* point to the next byte after it.

It's not so obvious how to do it and, more important, there are many ways to do it

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159