0

For example, the string is: namespace:name but I want to get only namespace, how can I do that? I tried this but I don't know how to output only the namespace

char id[] = "namespace:name";
char* token = strtok(id, ":");
while (token != NULL)
{
  printf("%s\n", token);
  token = strtok(NULL, ":");
}
  • 1
    You can't figure out how to only output the first token ? – John3136 Jul 09 '21 at 04:15
  • A quick thing to notice is the tag info for [tag:strtok], in particular the mention that it is a C function. Since you want C++, you should start by using [`std::string`](https://en.cppreference.com/w/cpp/string/basic_string) instead of C-style strings. – JaMiT Jul 09 '21 at 04:16
  • Or are you more interested in finding out how to change `while (token != NULL)` to `if (token != NULL)`? – JaMiT Jul 09 '21 at 04:18
  • I just want to output the namespace somehow, like for example, in Java we can just do `String[] id = identifier.split(":");` and then store them in variable like `String idName = id[1];` (Btw, identifier is supposed to be `namespace:name`). I want to do the same thing but in C++ – JstAsh01 Jul 09 '21 at 04:32

0 Answers0