Is There A Built-In Way to Split Strings In C++?

前端 未结 10 2215
心在旅途
心在旅途 2020-12-08 10:56

well is there? by string i mean std::string

10条回答
  •  温柔的废话
    2020-12-08 11:40

    C strings

    Simply insert a \0 where you wish to split. This is about as built-in as you can get with standard C functions.

    This function splits on the first occurance of a char separator, returning the second string.

    char *split_string(char *str, char separator) {
        char *second = strchr(str, separator);
        if(second == NULL)
            return NULL;
    
        *second = '\0';
        ++second;
        return second;
    }
    

提交回复
热议问题