"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Tokenize a `std::string` with C Functions?

How to Tokenize a `std::string` with C Functions?

Published on 2024-11-10
Browse:225

How to Tokenize a `std::string` with C Functions?

Tokenizing std::string with C Functions

Tokenizing a string is a fundamental operation in programming. However, when working with C functions like strtok(), which require a char* string, directly tokenizing a std::string can be met with challenges.

strtok() with std::string

To utilize strtok() with a std::string, one option is to convert it to a const char* using .c_str(). However, this may not always be desirable, as it provides a read-only representation of the string.

Alternative Approaches

A more suitable solution is to leverage std::istringstream instead of strtok(). std::istringstream allows for stream-based tokenization of a std::string. Here's an example:

#include 
#include 
#include 

int main() {
    std::string myText("some-text-to-tokenize");
    std::istringstream iss(myText);
    std::string token;

    while (std::getline(iss, token, '-')) {
        std::cout 

This code creates an std::istringstream from the std::string and reads tokens from it until it encounters the specified delimiter ('-' in this case).

Additional Options

For more advanced tokenization capabilities, libraries like Boost provide comprehensive solutions that offer greater flexibility and features compared to strtok().

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3