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.

2
  • Check out my function to split at any string. Commented Sep 7, 2015 at 21:29
  • @user3121023 strstr() is not sufficient - he wants to split the entire string, not just find the first occurrence. Commented Sep 7, 2015 at 21:38

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

thanks for the answer. I will check out that link. Btw I didn't downvote you.
@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 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
@TimCastelijns Thanks for the info, I had not noticed that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.