I have to split a sample string using strok
function using C++.
The sample string is: "This|is||a||sample||string|"
, while split this using strok
normally.
#include <stdio.h>
#include <string>
#include <string.h>
using namespace std;
int main()
{
string str="This||a||sample||string|";
string a;
str=strtok ((char *)str.c_str(),"|");
while (str.c_str() != NULL)
{
printf ("str:%s\n",str.c_str());
str = strtok (NULL, "|");
}
return 0;
}
Result:
str:This
str:a
str:sample
str:string
While changing the same string into "This| |a| |sample| |string|"
gives the expected result:
str:This
str:
str:a
str:
str:sample
str:
str:string
How can I get the expect result without changing the string?