简体   繁体   中英

How to split strings in C++ like in python?

so in python you can split strings like this:

string = "Hello world!"
str1 , str2 = string.split(" ") 
print(str1);print(str2)

and it prints:

Hello 
world!

How can i do the same in C++? This wasn't useful Parse (split) a string in C++ using string delimiter (standard C++) , i need them splited so i can acces them separatedly like print just str1 for example.

If your tokenizer is always a white space ( " " ) and you might not tokenize the string with other characters (eg s.split(',') ), you can use string stream:

#include <iostream>
#include <string>
#include <stringstream>

int main() {
    std::string my_string = " Hello   world!  ";
    std::string str1, str2;
    std::stringstream s(my_string);

    s>>str1>>str2;

    std::cout<<str1<<std::endl;
    std::cout<<str2<<std::endl;
    return 0;
}

Keep in mind that this code is only suggested for whitespace tokens and might not be scalable if you have many tokens. Output:

Hello
World!

Using boost:

#include <iostream>
#include <boost/algorithm/string.hpp>
#include <vector>

int main()
{
    std::string line;
    while(std::getline(std::cin, line)) {
        std::vector<std::string> v;
        boost::split(v, line, boost::is_any_of(" "));
        for (const auto& s : v) {
            std::cout << s << " - ";
        }
        std::cout << '\n';
    }
    return 0;
}

https://godbolt.org/z/EE3xTavMr

#include<bits/stdc++.h>
using namespace std;
int main(){
    string str ="123/43+2342/23432";
    size_t found = str.find("+");
    int b=found;
    int a,c;
    size_t found1 = str.find("/");
    if(found1!=string::npos){
        a = found1;
    }
    found1 = str.find("/",found1+1);
    if(found1!=string::npos){
        c = found1;
    }
    string tmp1 = str.substr(0,a-0);
    int num1 = stoi(tmp1);
    tmp1 = str.substr(a+1,b-a-1);
    int del1 = stoi(tmp1);
    tmp1 = str.substr(b+1,c-b-1);
    int num2 = stoi(tmp1);
    tmp1 = str.substr(c+1);
    int del2 = stoi(tmp1);

    cout<<num1<<" "<<del1<<" "<<num2<<" "<<del2<<endl;
    
    return 0;
}

Split all the number given in the string when you find "/" or "+".

You could create your own splitString function. Here is an example:

std::vector<std::string> splitString(std::string str, char splitter){
    std::vector<std::string> result;
    std::string current = ""; 
    for(int i = 0; i < str.size(); i++){
        if(str[i] == splitter){
            if(current != ""){
                result.push_back(current);
                current = "";
            } 
            continue;
        }
        current += str[i];
    }
    if(current.size() != 0)
        result.push_back(current);
    return result;
}

And here is an example usage:

int main(){ 
    vector<string> result = splitString("This is an example", ' ');
    for(int i = 0; i < result.size(); i++)
        cout << result[i] << endl;

    return 0;
}

Or how you want to use it:

int main(){
    vector<string> result = splitString("Hello world!", ' ');
    string str1 = result[0];
    string str2 = result[1];
    cout << str1 << endl;
    cout << str2 << endl;

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM