Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8370907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:01:39+00:00 2026-06-09T14:01:39+00:00

Possible Duplicate: Splitting a string in C++ I have this vector function from one

  • 0

Possible Duplicate:
Splitting a string in C++

I have this vector function from one of the answers I got on my question

vector<string> split(const string& s, char delim)
{
  vector<string> elems(2);
  string::size_type pos = s.find(delim);
  elems[0] = s.substr(0, pos);
  elems[1] = s.substr(pos + 1);
  return elems;
}

However, it only accept 2 elements. How do I modify it to accept based on how many delimiter s exist in the string s?

e.g if I have this:

user#password#name#fullname#telephone

sometime the size might differ.

How can I make this function flexible to work no matter how many elements, and able to split like this function above?

Just to further explain my problem:

What i wanna achieve is the capability to split using this vector function, of the same delimiter to N size instead of fixed at size 2.

This function can only split maximum 2 element in a string, more than that result in Segmentation core dump

as previously i only have needs for usage like

user:pass

now i added more attribute so i need to be able to split

user:pass:name:department

which x[2] and x[3] will return respectively name and department

they all will be using same delimiter.

Further Update:

I tried using this function provided by 1 of the answer below

vector<string> split(const string& s, char delim)
{
bool found;
vector<string> elems;
  while(1){
   string::size_type pos = s.find(delim);
   if (found==string::npos)break;
   elems.push_back(s.substr(0, pos));
   s=s.substr(pos + 1);
  }
  return elems;
}

and i get some error

server.cpp: In function ‘std::vector<std::basic_string<char> > split(const string&, char)’:
server.cpp:57:22: error: passing ‘const string {aka const std::basic_string<char>}’ as ‘this’ argument of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]’ discards qualifiers [-fpermissive]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:01:41+00:00Added an answer on June 9, 2026 at 2:01 pm

    There is one answer to the almost-duplicate that provides a generic split functionality:

    std::vector<std::string> split(std::string const& str, std::string const& delimiters = "#") {
      std::vector<std::string> tokens;
    
      // Skip delimiters at beginning.
      string::size_type lastPos = str.find_first_not_of(delimiters, 0);
      // Find first "non-delimiter".
      string::size_type pos = str.find_first_of(delimiters, lastPos);
    
      while (string::npos != pos || string::npos != lastPos) {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
      }
      return tokens;
    }
    

    which can be easily wrapped to:

    std::vector<std::string> split(std::string const& str, char const delimiter) {
      return split(str,std::string(1,delimiter));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: splitting a string i have a string which looks like this: http://pastebin.com/m5508ff19
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: Split string in SQL I have seen a couple of questions related
Possible Duplicate: Python: Split string with multiple delimiters I have a program where I
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: Why is String.Concat not optimized to StringBuilder.Append? One day I was ranting
Possible Duplicate: Splitting one column into two columns in SQL Server CE I am
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: Evaluate C# string with math operators So lets say I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.