This skin was created by Rocky of the IF Skin Zone


 

 Splitting Up A String, A basic example
Daniel
Posted: Jan 5 2007, 11:41 PM


Gamehawks Loyal Follower


Group: Members
Posts: 387
Member No.: 45
Joined: 16-January 06



I always wanted to know how to use the explode() function from PHP in C++. I have finally found a way. This is just another part of my server. So, please no flaming because it is useless by itself.

Heres the code:

CODE

#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

int main()
{
   string str = "000011222 X 2502";
   cout << str;
   cout << "\n";
   string buf;
   stringstream ss(str);m

   vector<string> tokens;

   while (ss >> buf)
   
   tokens.push_back(buf);
   

   cout<< "Messages handle is:";
   cout<<tokens[0]<<" ";
   cout<< "\n";
   
   cout<< "Messages variable is:";
   cout<<tokens[1]<<" ";
   cout<< "\n";
   
   cout<<"Messages variable update is:";
   cout<<tokens[2]<<" ";
   cout<<"\n";
   
   cin.get();
   
}



Cheers, Daniel


--------------------
1 pwn @ll y0u l0s3rs! G0 j4ck 0ff 1n4 a cup!
Top
Stenny
Posted: Jan 6 2007, 11:28 AM


C++ Composer


Group: Members
Posts: 475
Member No.: 49
Joined: 17-January 06



And what does the explode() function in PHP do?

-Stenny


--------------------
user posted image

BLOOP!
Top
Daniel
Posted: Jan 8 2007, 02:29 AM


Gamehawks Loyal Follower


Group: Members
Posts: 387
Member No.: 45
Joined: 16-January 06



Basically if I have a string from a file that contains "Stenny|password|25.07.81" Then I can put that through Explode (like so):

CODE


string = "Stenny|password|25.07.81";

explode('string', '|');


And itll output an array with 3 elements. Each element with be split up by a |. So element 1 will be Stenny.

- Cheers, Daniel


--------------------
1 pwn @ll y0u l0s3rs! G0 j4ck 0ff 1n4 a cup!
Top
BGamed
Posted: Apr 2 2007, 08:25 PM


Elite Member


Group: Members
Posts: 260
Member No.: 24
Joined: 29-December 05



I've just found a little bug, must be a mistake,
stringstream ss(str);m
you need to remove the "m" at the end of the line smile.gif
I assume it happened while you were posting it on the forum...

And em, I don't understand the point of this(also don't know how it works, could you explain?)

at least, how do I use it?

This post has been edited by BGamed on Apr 2 2007, 08:26 PM


--------------------
user posted imageuser posted image
Top
Cardinal
Posted: Apr 3 2007, 08:21 AM


The Lizard King


Group: Members
Posts: 244
Member No.: 34
Joined: 6-January 06



Cool, kind of like a strtok.

BG, you would whack a string (blah|black|sheep) in there and a char like '|' as he shows. Once you ran it you would get:

string[0] = blah
string[1] = black
string[2] = sheep

It just splits your string up when it finds the char you put in as the '|'.

EDIT[]

The strtok I'm talking about:

CODE

strtok(const string[], &index)
{
new length = strlen(string);
while ((index < length) && (string[index] <= ' '))
 {
  index++;
 }
new offset = index;
new result[20];
while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
 {
   result[index - offset] = string[index];
  index++;
 }
result[index - offset] = EOS;
return result;
}


Pawn version ^^^. I don't do c++ so I aint got a clue what the would look like in c++, I'll leave you to think about that one...

This post has been edited by Cardinal on Apr 3 2007, 08:27 AM


--------------------
Aiming for heaven in 2007......
user posted image
Top
MoroJr
Posted: Oct 26 2009, 07:55 PM


New


Group: Members
Posts: 4
Member No.: 224
Joined: 24-July 08



No no no ... that's strtok , not split

Split - Pawn Version
CODE

public split(const strsrc[], strdest[][], delimiter)
{
new i, li;
new aNum;
new len;
while(i <= strlen(strsrc))
{
    if(strsrc[i] == delimiter || i == strlen(strsrc))
 {
        len = strmid(strdest[aNum], strsrc, li, i, 128);
        strdest[aNum][len] = 0;
        li = i+1;
        aNum++;
 }
 i++;
}
return 1;
}
Top


Topic Options



Hosted for free by InvisionFree (Terms of Use: Updated 7/7/05) | Powered by Invision Power Board v1.3 Final © 2003 IPS, Inc.
Page creation time: 0.6075 seconds | Archive