Skip to main content
Spelling fixes
Source Link
Toby Speight
  • 88.4k
  • 14
  • 104
  • 327

Splitting std::string based on delimeterdelimiter using only find and substr

I have been playing around with these two functions for a bit, dunno if this is practical though. I have only been learning c++C++ for a a couple of days, coming from a javaJava background.

There we had a function that does this for us,us; I tried making something similar.

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

using namespace std;

vector<string>parse(string test, string Deli);
int main()
{

vector<string> x = parse("random text to test splitting apart ", " ");
// note , the deliminator have to be after the text not before it.
for (string &e : x)
{
    cout << e << endl;
}
return 0;
}
vector<string>parse(string test, string Deli) {
    int count = 0; int token = 0;
    vector<string>parsed;
    for (size_t i = 0; i <= count; i++)
    {
        string x = (test.substr(token, test.find(Deli, token)-token));
        parsed.push_back(x);
        token += test.find(Deli, token +1) - (token-1);
        test.find(Deli, token) != std::string::npos ? count++ : count;
    }
    return parsed;
}

Splitting std::string based on delimeter using only find and substr

I have been playing around with these two functions for a bit, dunno if this is practical though. I have only been learning c++ for a a couple days, coming from a java background.

There we had a function that does this for us, I tried making something similar.

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

using namespace std;

vector<string>parse(string test, string Deli);
int main()
{

vector<string> x = parse("random text to test splitting apart ", " ");
// note , the deliminator have to be after the text not before it.
for (string &e : x)
{
    cout << e << endl;
}
return 0;
}
vector<string>parse(string test, string Deli) {
    int count = 0; int token = 0;
    vector<string>parsed;
    for (size_t i = 0; i <= count; i++)
    {
        string x = (test.substr(token, test.find(Deli, token)-token));
        parsed.push_back(x);
        token += test.find(Deli, token +1) - (token-1);
        test.find(Deli, token) != std::string::npos ? count++ : count;
    }
    return parsed;
}

Splitting std::string based on delimiter using only find and substr

I have been playing around with these two functions for a bit, dunno if this is practical though. I have only been learning C++ for a couple of days, coming from a Java background.

There we had a function that does this for us; I tried making something similar.

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

using namespace std;

vector<string>parse(string test, string Deli);
int main()
{

vector<string> x = parse("random text to test splitting apart ", " ");
// note , the deliminator have to be after the text not before it.
for (string &e : x)
{
    cout << e << endl;
}
return 0;
}
vector<string>parse(string test, string Deli) {
    int count = 0; int token = 0;
    vector<string>parsed;
    for (size_t i = 0; i <= count; i++)
    {
        string x = (test.substr(token, test.find(Deli, token)-token));
        parsed.push_back(x);
        token += test.find(Deli, token +1) - (token-1);
        test.find(Deli, token) != std::string::npos ? count++ : count;
    }
    return parsed;
}
Clarified title of the post
Link
Incomputable
  • 9.7k
  • 3
  • 34
  • 73

Splitting Text Code c++std::string based on delimeter using only find and Substr?substr

edited tags
Link
added the comment about how to format the string
Source Link
Noor
  • 73
  • 1
  • 7
Loading
added 24 characters in body
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141
Loading
deleted 25 characters in body
Source Link
Loading
Source Link
Noor
  • 73
  • 1
  • 7
Loading