1

I have a string, and need it split into a string array. The string is like this Line 0|X|X|1|x|2|3|X, there is a few of these and there will always be 9 values into the array, with | being used as a splitter

I'm struggling to find something that isn't completely over complicated, so any help would be apperciated

Craig Weston
  • 141
  • 2
  • 4
  • 10
  • looking at it that is massively over complex, I need a simple solution, that would be easy for someone learning to understand, I do web development in the past. – Craig Weston Apr 05 '14 at 11:37
  • @CraigWeston *massively over complex*? what are you talking about. The highest ranked answer (unfortunately not the accepted one) gives a nice and short function `split()`, using `std::getline` (as David's answer below). – Walter Apr 05 '14 at 11:43

1 Answers1

4

I believe this is what you want

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

int main() {

    std::string input = "0|X|X|1|x|2|3|X";
    std::istringstream ss(input);
    std::string token;

    while(std::getline(ss, token, '|')) {
       std::cout << token << '\n';
    }

    return 0;
}

http://ideone.com/ODVumC

getline can work with any string stream and accepts an arbitrary separator

Marco A.
  • 43,032
  • 26
  • 132
  • 246