Parse a string with WxWidgets

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Experienced Solver
Experienced Solver
Posts: 55
Joined: Sat Jul 27, 2019 3:45 pm

Parse a string with WxWidgets

Post by marcusbarnet » Thu Aug 29, 2019 8:58 am

I'm developing a WxWidgets application under Ubuntu and I'm using CodeBlocks as IDE.

I would like to:
  • read the data stored in a magnetic card band
  • check if they are OK
  • parse the string in order to get the code, the name and the surname

I'm using a USB magnetic card reader and I set a TextCtrl as "focused" so it immediately gets the data from the reader automatically.

The format of the string is like this:

%URSMCC84R04E815LURSO MARCO_ò8038001605011036724_

The control characters are always the same and I need to read everything between % and _.
I need to obtain three strings:
  • the first one containing the first 16 chars
  • the second contains the surname which goes from the 17th till the first space
  • the third string contains the name and goes from the first char after the space till the _

What is the best way to do this in Wxwidgets?
Moreover, since the reader emulates a keyboard, the output string is read a char at a time, so how can I design the parsing code?

This is the code I'm using to read the string.

Code: Select all

void ProjectFrame::OntesseraText(wxCommandEvent& event)
    wxString stringa_tessera = tessera->GetValue(); // I read the string coming from the USB reader
    TextCtrl2->ChangeValue( stringa_tessera ); // I load the string in the second TextCtrl just for test

void ProjectFrame::OnTextCtrl2Text(wxCommandEvent& event)
// TextCtrl used to display the parsed string
I see that there is a class ... nizer.html, should I use it?
I'm sorry but I'm just a beginner :(

Thank you!

User avatar
Posts: 14427
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Parse a string with WxWidgets

Post by doublemax » Thu Aug 29, 2019 9:32 am

The best way would be to use regular expressions:

If you've never used them before, the syntax can be quite confusing and overwhelming, but you can use this in almost any programming language and you'll always need it, sooner or later. So it's good to learn this.

As the structure of the string is pretty simple, you could solve it using methods like wxString::SubString(), AfterFirst, AfterLast, BeforeFirst, BeforeLast etc. But i would recommend to use regular expressions.

wxTokenizer is not the right tool for this job.
Use the source, Luke!

Post Reply