c++字符串分割问题

c++string字符串分割问题
有以下字符串:
THEN(a,b,WHEN(c,THEN(e,f)),m,n)
想分割成如下的形式:
a,
b,
WHEN(c,THEN(e,f)),
m,
n


#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "THEN(a,b,WHEN(c,THEN(e,f)),m,n)";
    int start = 0;
    while (start < str.length()) {
        int pos = str.find(",", start);
        if (pos == -1) pos = str.length();
        cout << str.substr(start, pos - start) << endl;
        start = pos + 1;
    }
    return 0;
}


可以使用以下代码:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    string str = "THEN(a,b,WHEN(c,THEN(e,f)),m,n)";
    vector<string> result;
    int start = 0;
    int end = 0;
    int count = 0;
    for (int i = 0; i < str.length(); i++)
    {
        if (str[i] == '(')
        {
            count++;
        }
        else if (str[i] == ')')
        {
            count--;
        }
        else if (str[i] == ',' && count == 0)
        {
            end = i;
            result.push_back(str.substr(start, end - start));
            start = end + 1;
        }
    }
    result.push_back(str.substr(start, str.length() - start));
    for (int i = 0; i < result.size(); i++)
    {
        cout << result[i] << endl;
    }
    return 0;
}

在C++中,可以使用以下几种方法来分割字符串:
1.使用STL库中的stringstream:可以使用stringstream将字符串转换为流,然后使用>>操作符从流中读取单个字符串。
2.使用strtok函数:strtok函数可以按给定的分隔符分割字符串。
3.自定义函数:可以编写自定义函数,扫描字符串,并按给定的分隔符查找字符串的位置,然后创建子字符串。
下面是使用stringstream的代码示例:

#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main() {
    string str = "This is a test string";
    string word;
    vector<string> words;
    stringstream ss(str);

    while (ss >> word) {
        words.push_back(word);
    }

    for (string w : words) {
        cout << w << endl;
    }

    return 0;
}


```c
This
is
a
test
string


```

字符串分割

可以使用以下代码:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<string> split(string s) {
    vector<string> result;
    int start = 0;
    int end = 0;
    int count = 0;
    while (end < s.length()) {
        if (s[end] == '(') {
            count++;
        } else if (s[end] == ')') {
            count--;
        } else if (s[end] == ',' && count == 0) {
            result.push_back(s.substr(start, end - start));
            start = end + 1;
        }
        end++;
    }
    result.push_back(s.substr(start, end - start));
    return result;
}

int main() {
    string s = "THEN(a,b,WHEN(c,THEN(e,f)),m,n)";
    vector<string> result = split(s);
    for (int i = 0; i < result.size(); i++) {
        cout << result[i] << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
string s = "THEN(a,b,WHEN(c,THEN(e,f)),m,n)";
int main() {
    int pairs = 0;
    string parts = "";
    for (int i = 0; i < s.size(); i ++) {
        if (s[i] == '(') {
            pairs ++;
            if (pairs != 1)
                parts += s[i];
        } else if (s[i] == ')') {
            pairs --;
            if (pairs != 0)
                parts += s[i];
        } else if (pairs != 0) parts += s[i];
        if (pairs == 1 && s[i] == ',') {
            parts.erase(parts.size() - 1);
            cout << parts << endl;
            parts = "";
        }
    }
    cout << parts << endl;
    return 0;
}
#include <stdio.h>
#include <string.h>

int main(){    
    const char* s = "THEN(a,b,WHEN(c,THEN(e,f)),m,n)";
    const char* token = strchr(s, '('); //先找到第一个括号
    int token_len = 0;
    int level = 1;
    if(!token)return -1;
    token ++;
    s = token;
    while(*s){        
        if(*s == '('){ //碰到( 层级+1
            level ++;            
        }else if(*s == ')'){ //碰到) 层级-1
            level --;
        }        
        if(level == 1 && *s == ','){ //第一层级中,碰到,结束
            printf("%.*s\n", token_len + 1, token);
            token = s + 1; 
            token_len = 0;                    
        }else if(level == 0 && *s == ')'){ //已经最外层,碰到)结束
            printf("%.*s\n", token_len, token);            
        }else{
            token_len ++;
        }        
        s ++;
    }    
    return 0;
}

该回答引用ChatGPT
代码如下

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> split(string str, char separator) {
    vector<string> result;
    string sub_str;
    int bracket_count = 0;
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == '(') {
            bracket_count++;
        }
        if (str[i] == ')') {
            bracket_count--;
        }
        if (str[i] == separator && bracket_count == 0) {
            result.push_back(sub_str);
            sub_str.clear();
            continue;
        }
        sub_str += str[i];
    }
    result.push_back(sub_str);
    return result;
}

int main() {
    string str = "THEN(a,b,WHEN(c,THEN(e,f)),m,n)";
    vector<string> result = split(str, ',');
    for (int i = 0; i < result.size(); i++) {
        cout << result[i] << endl;
    }
    return 0;
}


在C++中分割字符串的方法有很多,其中一种是使用STL库的stringstream和getline函数。
以下是一个示范代码,可以帮助您完成字符分

img

上面的代码使用了 stringstream 和 getline 函数将输入字符拆分为单个字符并存储在 vector 容器中。您可以修改代码以符合您的需求

首先,您可以使用C ++的string函数来实现字符串分割。您可以使用string.find()和string.substr()函数找到您想要分割字符串的子字符串,并將它提取到新的字符串中。您需要使用一个循环来遍历整个字符串,并记录每次开始分割的位置,然后使用字符', '作为分隔符。最后您可以使用string.substr()函数来提取每个分割后的子字符串。


#include <iostream>
#include <string>

using namespace std;

int balance(const string &s, int start) {
    int balance = 1;
    for (int i = start + 1; i < s.size(); i++) {
        if (s[i] == '(') {
            balance++;
        }
        if (s[i] == ')') {
            balance--;
        }
        if (balance == 0) {
            return i;
        }
    }
    return -1;
}

void split(const string &s) {
    int start = s.find("(") + 1;
    int end = 0;
    while (end < s.size()) {
        end = s.find(",", start);
        if (end == string::npos) {
            end = s.size();
        }
        if (s[start] == '(') {
            int matching = balance(s, start);
            cout << s.substr(start, matching - start + 1) << endl;
            start = matching + 1;
        } else {
            cout << s.substr(start, end - start) << endl;
            start = end + 1;
        }
    }
}

int main() {
    string s = "THEN(a,b,WHEN(c,THEN(e,f)),m,n)";
    split(s);
    return 0;
}



您可以使用string类的find()函数和substr()函数来实现:
如果有帮助到您,希望可以采纳,谢谢!

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str = "THEN(a,b,WHEN(c,THEN(e,f)),m,n)";
    int pos = 0;
    while(pos != -1)
    {
        int start = str.find('(', pos);
        int end = str.find(')', pos);
        if(start != -1 && end != -1)
        {
            string subStr = str.substr(start+1, end-start-1);
            cout << subStr << endl;
            pos = end + 1;
        }
        else
        {
            pos = -1;
        }
    }
    return 0;
}

字符串分割,方式很多啊,比如:

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    //char str[]="a,b,c,d";
    //如果输入是string要转成char数组[strcpy(buf,str.c_str())]
    //或者用const_cast转换成char指针
    string str="a,b,c,d";
    const char *delim=",";
    char *p=strtok(const_cast<char *>(str.c_str()),delim);
    while(p)
    {
        cout<<p<<endl;
        p=strtok(NULL,delim);
    }
    return 0;
}


C++中的字符串分割可以通过 STL 中的 stringstream 和 getline 函数来实现。下面是一个使用 stringstream 和 getline 实现字符串分割的代码示例:

img

运行上述代码,得到的结果如下:

img


此代码将字符串分割成了指定的形式,每个分割后的字符串都存储在一个 vector 中。

then(),只是分割部分么

在C++中,可以使用字符串流stringstream和getline函数来实现字符串的切割。具体实现步骤如下:

1.将要切割的字符串输入到字符串流中。
2.使用getline函数从字符串流中读取一个子串,直到达到指定的分隔符。
3.将读取的子串存储到一个容器中,如vector
4.重复步骤2和步骤3,直到字符串流中的子串全部读取完毕。

以下是一个示例程序,演示如何使用stringstreamgetline函数来切割字符串:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

int main() {
    std::string input = "Hello,world,foo,bar";
    std::stringstream ss(input);  // 将字符串输入到字符串流中
    std::vector<std::string> tokens;  // 存储子串的容器
    std::string token;

    while (std::getline(ss, token, ',')) {  // 以逗号为分隔符,从字符串流中读取子串
        tokens.push_back(token);  // 将子串存储到容器中
    }

    for (const auto& t : tokens) {
        std::cout << t << std::endl;  // 输出子串
    }

    return 0;
}

在这个示例程序中,我们首先定义了一个字符串iput,其中包含了多个逗号分隔的子串。然后,我们创建了一个字符串流ss,并将iput输入到字符串流中。接着,我们定义了一个vector容器tokens,用于存储从字符串中读取的子串。然后,我们使用getline函数从字符串流ss中读取子串,并以逗号为分隔符。每次读取完一个子串后,我们将其存储到tokens容器中。最后,我们遍历tokens`容器,输出其中存储的子串。

当程序运行时,将输出以下内容:

Hello
world
foo
bar

可以看到,我们成功地将原始字符串切割成了多个子串,并将它们存储到了容器中。