Delimiters (token term also used) are characters which separates strings between each of them. For example spaces, comas and other symbols can be used as a delimiter char to separate strings between them . Normaly in C++ strtok() were being used in C++, that is used with chars. In Modern C++, strings are now UnicodeStrings, TStringlists has some property to extract strings. If you are new to UnicodeStrings please check here
Example procedure below shows how to split a UnicodeString to a StringList which has delimited UnicodeStrings
1 2 3 4 5 6 7 8 9 |
void split(wchar_t delimiter, UnicodeString str, TStringList *listofstrings) { listofstrings->Clear(); listofstrings->Delimiter = delimiter; listofstrings->StrictDelimiter = True; // Requires D2006 or newer. listofstrings->DelimitedText = str; } |
We can easily use this In C++ Builder. Create a new “Multi-Device C++ Builder Project” , Add a Memo and a Button on the Form of Project. Double click to Button, add this split procedure and OnClick() event of Button as below;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <fmx.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void split(wchar_t delimiter, UnicodeString str, TStringList *listofstrings) { listofstrings->Clear(); listofstrings->Delimiter = delimiter; listofstrings->StrictDelimiter = True; // Requires D2006 or newer. listofstrings->DelimitedText = str; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { UnicodeString us=L"Hello,this is test,with comas,done"; TStringList *strlist= new TStringList(); split( L',', us, strlist); Memo1->Lines->Clear(); Memo1->Lines->Add(strlist->Text); } |
Header of this project will be automatically like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <System.Classes.hpp> #include <FMX.Controls.hpp> #include <FMX.Forms.hpp> #include <FMX.Controls.Presentation.hpp> #include <FMX.Memo.hpp> #include <FMX.Memo.Types.hpp> #include <FMX.ScrollBox.hpp> #include <FMX.StdCtrls.hpp> #include <FMX.Types.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TMemo *Memo1; TButton *Button1; void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif |
We can use any UnicodeString Chars as given above in L’ ‘ format. This method supports UnicodeStrings, because of this it is much more modern than classic strtok() function. Instead of this split(…) definition, we can also define this function as a member of Form like this;
1 2 3 4 5 6 |
void TForm1::split(wchar_t delimiter, UnicodeString str, TStringList *listofstrings) { ... } |
This time this split procedure should be defined as a a public member in public: definitions of Form1 class as below;
1 2 3 4 5 |
public: // User declarations __fastcall TForm1(TComponent* Owner); void split(wchar_t delimiter, UnicodeString str, TStringList *listofstrings); |
Using Delimiters globally in all languages requires to use of UnicodeStrings, and sometimes analyzing these text forms highly needs this kind of new modern methods. We think that this example will help you to analyze Unicode text forms.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition