How to split string using istringstream with other delimiter than whitespace?

The following trick using istringstream to split a string with white spaces.

int main() { string sentence("Cpp is fun"); istringstream in(sentence); vector<string> vec = vector<string>(istream_iterator<string>(in), istream_iterator<string>()); return 0; }

Is there a similar trick to split a string with any delimiter? For instance, | in "Cpp|is|fun".

-------------Problems Reply------------

Generally speaking the istringstream approach is slow/inefficient and requires at least as much memory as the string itself (what happens when you have a very large string?). The C++ String Toolkit Library (StrTk) has the following solution to your problem:

#include <string>
#include <vector>
#include <deque>
#include "strtk.hpp"
int main()
std::string sentence1( "Cpp is fun" );
std::vector<std::string> vec;
strtk::parse(sentence1," ",vec);

std::string sentence2( "Cpp,is|fun" );
std::deque<std::string> deq;

return 0;

More examples can be found Here

Category:c# Views:0 Time:2010-12-26
Tags: c#

Related post

  • Splitting strings through regular expressions by punctuation and whitespace etc in java 2011-09-12

    I have this text file that I read into a Java application and then count the words in it line by line. Right now I am splitting the lines into words by a String.split([\\p{Punct}\\s+])" But I know I am missing out on some words from the text file. Fo

  • Split String into an array of String 2011-05-22

    I'm trying to find a way to split a String into an array of String(s), and I need to split it whenever a white spice is encountered, example "hi i'm paul" into" "hi" "i'm" "paul" How do you represent white spaces in split() method using RegularExpres

  • Splitting String C++ 2008-11-09

    Hi in C++ if i have a string, how can I split this into tokens? --------------Solutions------------- this works nicely for me :), it puts the results in elems. delim can be any char. std::vector<std::string> &split(const std::string &s,

  • How do I split strings in J2ME? 2008-10-14

    How do I split strings in J2ME in an effective way? There is a StringTokenizer or String.split(String regex) in the standard edition (J2SE), but they are absent in the micro edition (J2ME, MIDP). --------------Solutions------------- There are a few i

  • Split string containing command-line parameters into string[] in C# 2008-11-18

    I have a single string that contains the command-line parameters to be passed to another executable and I need to extract the string[] containing the individual parameters in the same way that C# would if the commands had been specified on the comman

  • split string on a number of different characters 2008-12-17

    I'd like to split a string using one or more separator characters. E.g. "a b.c", split on " " and "." would give the list ["a", "b", "c"]. At the moment, I can't see anything in the standard library to do this, and my own attempts are a bit clumsy. E

  • How to split strings into text and number? 2009-01-09

    I'd like to split strings like these 'foofo21' 'bar432' 'foobar12345' into ['foofo', '21'] ['bar', '432'] ['foobar', '12345'] Does somebody know an easy and simple way to do this in python? --------------Solutions------------- I would approach this b

  • Split string logic in J2ME 2009-03-18

    I am developing a J2ME application. I want to split the following string at "<br>" & comma: 3,toothpaste,2<br>4,toothbrush,3 How can I do this? --------------Solutions------------- private String[] split(String original,String separat

  • Split string into a list in Python 2009-04-13

    I want my python function to split a sentence (input) and store each word in a list. The code that I've written so far splits the sentence, but does not store the words as a list. How do I do that? def split_line(text): # split the text words = text.

  • Regular expression, split string by capital letter but ignore TLA 2009-07-08

    I'm using the regex System.Text.RegularExpressions.Regex.Replace(stringToSplit, "([A-Z])", " $1").Trim() to split strings by capital letter, for example: 'MyNameIsSimon' becomes 'My Name Is Simon' I find this incredibly useful when working with enume

  • regexp split string by commas and spaces, but ignore the inside quotes and parentheses 2009-09-03

    I need split string by commas and spaces, but ignore the inside quotes, single quotes and parentheses $str = "Questions, \"Quote\",'single quote','comma,inside' (inside parentheses) space #specialchar"; so that the resultant array will have [0]Questi

  • linux shell script: split string, put them in an array then loop through them 2009-09-10

    Possible Duplicate: Split string based on delimiter in Bash? In a bash script how do I split string with a separator like ; and loop through the resulting array? --------------Solutions------------- You can probably skip the step of explicitly creati

  • Split string in lua? 2009-09-15

    I need to do a simple split of a string, but there doesnt seem to be a function for this, and the manual way i tested didn't seem to work. How would i do it? --------------Solutions------------- Please see Splitting Strings: Here are various ways of

  • Split string in 512 char chunks 2009-10-27

    Maybe a basic question but let us say I have a string that is 2000 characters long, I need to split this string into max 512 character chunks each. Is there a nice way, like a loop or so for doing this? --------------Solutions------------- Something

  • How to split strings on carriage return with C#? 2009-11-29

    I have an ASP.NET page with a multiline textbox called txbUserName. Then I paste into the textbox 3 names and they are vertically aligned: Jason Ammy Karen I want to be able to somehow take the names and split them into separate strings whenever i de

  • Regex help with splitting string 2009-12-19

    I need to be able to parse strings like these: kev-+kvs+-one-+gdl+-greg-+kvs+-two-+gdl+-les-+kvs+-three -+gdl+-kev-+kvs+-one-+gdl+-greg-+kvs+-two-+gdl+-les-+kvs+-three kev-+kvs+-one-+gdl+-greg-+kvs+-two-+gdl+-les-+kvs+-three-+gdl+- and in all three c

  • PHP: Split string into array, like explode with no delimiter 2010-01-31

    I have a string such as: "0123456789" and need to split EACH character into an array. I for the hell of it tried: explode('', '123545789'); But it gave me the obvious: Warning: No delimiter defined in explode) .. How would I come across this? I can't

  • How to split string in group in 2010-04-09

    i'm amol kadam,I want to know how to split string in two part.My string is in Time format (12:12).& I want to seperate this in hour & minute format.the datatype for all variables are string. for hour variable used strTimeHr & for minute s

  • C++ problem with string stream istringstream 2010-04-17

    I am reading a file in the following format 1001 16000 300 12.50 2002 24000 360 10.50 3003 30000 300 9.50 where the items are: loan id, principal, months, interest rate. I'm not sure what it is that I am doing wrong with my input string stream, but I

Copyright (C), All Rights Reserved.

processed in 0.173 (s). 11 q(s)