【问题标题】:How do you split the string using specific number?如何使用特定数字拆分字符串?
【发布时间】:2021-06-16 10:34:11
【问题描述】:

我正在尝试使用特定数字“11”来拆分数字。例如,“1110110011”是一个字符串,我想创建一个这样的字符串数组,“11,1011,0011”。谁能帮我?我正在为这个函数使用 C++。

#include <vector>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;


vector<string> splitStrings(string str, char dl){
    string word = "";

    // to count the number of split strings 
    int num = 0;

    // adding delimiter character at the end 
    // of 'str' 
    str = str + dl;

    // length of 'str' 
    int l = str.size();

    // traversing 'str' from left to right 
    word = "";
    vector<string> substr_list;
    for (int i = 0; i < l; i++) {
        // if str[i] is not equal to the delimiter 
        // character then accumulate it to 'word' 
        if (str[i] == dl && str[i + 1] == dl) {
            //cout << str[i] << str[i+1] << endl;
            word += str[i];
            //substr_list.push_back(word);
            word += str[i + 1];
            //substr_list.push_back(word);
            str[i] = 0;
            str[i + 1] = 0;
            if (word != "1" || word != "0" ) {
                substr_list.push_back(word);
            }
            word = "";
        }
        else {
            word += str[i];

        }
    }

    // return the splitted strings 
    return substr_list;
}

int main(){
    string str1 = "1110110011";
    char dl = '1';
    int numberofsplitstring = 3;

    vector<string> result = splitStrings(str1, dl);
    for (int i = 0; i < numberofsplitstring; i++) {
        cout << result[i]; //without space
    }
}

有一个缺陷我不知道如何修复。我的目的是确保当我打印结果时,它仍然出现“1110110011”。但是,当我在 Visual Studios 2019 中实现它时,它输出为“11 1011 0011”。因此,当我添加 endl 行时,它的输出是这样的,

This picture in this link. For the second and third element of the vector, 1011 and 0011 has an extra whitespace I needed to remove.

【问题讨论】:

  • 你使用的是字符数组还是整数数组?
  • 目前,我使用的是向量字符串。

标签: c++ arrays string split numbers


【解决方案1】:

我尝试了一些东西,但现在我必须走了。这就是为什么我要把我迄今为止所做的部分交给你。我希望你能从现在开始。

#include <iostream>
#include <bits/stdc++.h>
using namespace std;


// Function accepts a string and
// checks if string can be split.
void split(string str, string keyword){
    int len = str.length();
    // if there is only 1 number
    // in the string then
    // it is not possible to split it
    if (len == 1) {
        cout << ("Not Possible");
        return;
    }
    string* splitted = new string[2030] {};
    std::string tempString = str;
    for (int i = 0; i <= len; i++) {
        std::string check(1, str[i]);
        check += str[i+1];


        if (strcmp(check.c_str(), keyword.c_str()) == 0){

            cout << "\nEqual." <<check.c_str() <<keyword.c_str() << endl;
            tempString.erase(i, i+1);
            cout << tempString << endl;
        }else {
            cout << "\nNot equal." <<check.c_str() <<keyword.c_str() << endl;
        }

    }
}


int main()
{
    string str = "1110110011";

    // Call the split function
    // for splitting the string
    split(str, "11");

    return 0;
}

您可以查看here 的擦除功能。 您可以检查here 的字符串数组

【讨论】:

    【解决方案2】:

    使用字符串的find方法查找第一个匹配的位置。

    将子字符串从起始索引 (0) 添加到该位置+1(以考虑第二个 '1')

    调整搜索位置并重复。

    【讨论】:

      【解决方案3】:

      您可以使用拆分功能,然后在每个列表元素的末尾添加“11”

      https://www.w3schools.com/python/ref_string_split.asp

      【讨论】:

      • OP 询问的是 C++ 解决方案,而不是 python。
      猜你喜欢
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2015-12-18
      • 2018-09-20
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多