5 Replies - 831 Views - Last Post: 19 February 2010 - 04:51 PM Rate Topic: -----

#1 FinalVersion  Icon User is offline

  • New D.I.C Head

Reputation: -12
  • View blog
  • Posts: 43
  • Joined: 19-February 10

String Manipulation

Posted 19 February 2010 - 11:20 AM

Lets say I have a string "The brown fox jumped over the fence.", how can I Split the string by a given deliminator? For example if I gave " " (space) as a deliminator, it would put each word into an array. Also any String Manipulation reference would be useful.

I found http://www.cplusplus.../string/string/ but it doesn't appear to have what I want in there.

Is This A Good Question/Topic? 0
  • +

Replies To: String Manipulation

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: String Manipulation

Posted 19 February 2010 - 11:23 AM

You might try reading this excellent tutorial from Martyn.Rae, and telling us what language you're using.
Was This Post Helpful? 0
  • +
  • -

#3 FinalVersion  Icon User is offline

  • New D.I.C Head

Reputation: -12
  • View blog
  • Posts: 43
  • Joined: 19-February 10

Re: String Manipulation

Posted 19 February 2010 - 11:27 AM

C++.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: String Manipulation

Posted 19 February 2010 - 11:29 AM

OK, so go read the tutorial. You might find it helpful.
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: String Manipulation

Posted 19 February 2010 - 11:41 AM

Are you putting the tokens into char* arrays? Use strtok() with " " as your delimiter.
Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: String Manipulation

Posted 19 February 2010 - 04:51 PM

If you only have one delimiter, then getline from a stringstream might be the simplest - the default delimiter is '\n' (a newline character) - but you can specify a different one.
#include <string>
#include <sstream>
#include <vector>
#include <iostream>

int main()
{
    std::vector<std::string> vec;
    std::string token, str
        = "The quick brown fox jumps over the lazy dog";
    std::istringstream buffer(str);

    while( std::getline(buffer, token, ' ') )
        vec.push_back(token);
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1