Форум АНТИЧАТ

Форум АНТИЧАТ (https://forum.antichat.xyz/index.php)
-   С/С++, C#, Delphi, .NET, Asm (https://forum.antichat.xyz/forumdisplay.php?f=24)
-   -   строки в C++ срочно (https://forum.antichat.xyz/showthread.php?t=211572)

77org77 15.06.2010 00:55

строки в C++ срочно
 
Привет всем у меня такой вопрос по C++ Ксть какая небуть сандартная функция чтобы поделить строку по кусочкам например строка Имя|фамилия|Отчество|Дата и всё это разделить например на массивы.

Ra$cal 15.06.2010 01:01

нету

77org77 15.06.2010 01:18

Жалко а как можно поделить строку

Ins3t 15.06.2010 01:30

Разбить вашу строку можно сишной функцией strtok библиотеки string.h

Пример использования strtok.

Код:

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
#include <iostream>
using std::cout;
using std::endl;
//////////////////////////////////////////////////////////////////////////
#include <cstring>
using std::strtok;
//////////////////////////////////////////////////////////////////////////
int main( void )
        {
        char sentence[] = "Name|First Name|Blabla|lalala";
        char *tokenPtr;

        tokenPtr = strtok( sentence, "|" );

        while( tokenPtr != NULL )
                {
                cout << tokenPtr << endl;
                tokenPtr = strtok( NULL, "|" );
                }

        return 0;
        }
//////////////////////////////////////////////////////////////////////////

Или с помощью библиотеки С++ string:

Код:

//////////////////////////////////////////////////////////////////////////
#include <iostream>
using std::cout;
using std::endl;
//////////////////////////////////////////////////////////////////////////
#include <string>
using std::string;
//////////////////////////////////////////////////////////////////////////
int main( void )
        {
        string myString( "Name|First Name|Blabla|lalala" );
        size_t found;

        found = myString.find_first_of( "|" );

        while( found != string::npos )
                {
                myString[ found ] = '\n';
                found = myString.find_first_of( "|", found + 1 );
                }

        cout << myString << endl;

        return 0;
        }
//////////////////////////////////////////////////////////////////////////


Самовар 15.06.2010 03:01

Блин, а мне так нравилась explode из PHP

Ra$cal 15.06.2010 03:14

http://blog.vernart.ru/programming/cplusplus/135/
в плюсах тож не сложно, если буст юзать.


Время: 18:45