Results 1 to 2 of 2
  1. #1
    pasmpasm
    pasmpasm is offline
    Guest
    Join Date
    2013 May
    Posts
    1
    Thanks Thanks Given 
    0
    Thanks Thanks Received 
    0
    Thanked in
    0 Posts
    Rep Power
    0

    [C++] Easiest way to split a string

    Function, to split a string

    PHP Code:
    #include <windows.h>
    #include <vector>

    void Split(const charpszStrIn, const char delimiterstd::vector<char*>* vecStrOut)
    {
        
    int        nStrInLen
        
    int        nFoundPos;
        
        
    char*    pszTmp;
        
    int        nTmpLen;

        
    nStrInLen    strlen(pszStrIn);
        
    nFoundPos   0;

        for (
    int i=0i<nStrInLen; )
        {
            
    nFoundPos 0;

            
    // ----------------------------------------
            // Find Delimiter
            // ----------------------------------------
            
    for (int ii=iii<nStrInLenii++)
            {
                if ( (
    pszStrIn[ii] == delimiter) )
                {
                    
    nFoundPos ii;
                    break;
                }
            }

            if (
    nFoundPos 1)
                
    nFoundPos nStrInLen;


            
    // ----------------------------------------
            // push vector
            // ----------------------------------------
            
    nTmpLen = (nFoundPos-i)+1;
            
    pszTmp = new char[nTmpLen];

            
    memset(pszTmp0nTmpLen);
            
    memcpy(pszTmp, (char*)(pszStrIn+i), nTmpLen-1);
            
    vecStrOut->push_back(pszTmp);

            
    += ((nFoundPos-i) + 1);
        }
    }

    void FreeSplit(std::vector<char*>* vecStr)
    {
        for (
    std::vector<char*>::iterator it vecStr->begin(); 
            
    it!=vecStr->end(); ++it) {
            
    delete *it;
        }

    Usage.
    PHP Code:
        std::vector<char*> str;

        
    Split("Hello, how are you ?"' ', &str);

        for (
    u_int i=0i<str.size(); i++)
            
    printf("%s\n"str[i]);

        
    FreeSplit(&str); 
    Result.
    PHP Code:
    Hello,
    how
    are
    you

    "It's So Easy"

  2. #2
    hohoha
    hohoha is offline
    Guest
    Join Date
    2014 Apr
    Posts
    2
    Thanks Thanks Given 
    0
    Thanks Thanks Received 
    0
    Thanked in
    0 Posts
    Rep Power
    0
    use strtk,very fast,and include more function

Similar Threads

  1. [VB] String to Hex / Hex to String
    By The_USDL in forum VB, .NET Framework
    Replies: 3
    Last Post: 2013-04-19, 04:37 PM
  2. Easiest way to level up in Dragon Nest SEA
    By deathcatharsis in forum Dragon Nest
    Replies: 0
    Last Post: 2012-09-10, 08:12 AM
  3. [C++] Easiest way to Hide Injected DLL in Windows
    By Grooguz in forum C/C++
    Replies: 0
    Last Post: 2012-03-04, 04:53 PM
  4. String to Int . Need help.
    By pohkak in forum C/C++
    Replies: 0
    Last Post: 2011-09-02, 01:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •