wxString spliting syntax

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
paddle
Knows some wx things
Knows some wx things
Posts: 43
Joined: Sat Oct 12, 2019 4:57 pm

wxString spliting syntax

Post by paddle »

I have a wxString formated as follow :

Code: Select all

(WxString1:WxString2)
And I'm trying to get two wxStrings: WxString1 and WxString2

Code: Select all

if(mwxString[0].GetValue() == "("){
                int colonIndex=0;
                for(int i = ;, i<mwxString.size(); i++){
                    if(mwxString[i].GetValue() == ":"){
                        colonIndex=i;
                        i=mwxString.size();
                    }
                }

                wxString WxString1 = mwxString[1,colonIndex-1];
                wxString WxString2= mwxString[colonIndex+1,WxID.size()];
            }
First my syntax to find the "(" is not working.

Code: Select all

error: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
And I don't think the syntax "mwxString[1,colonIndex-1]" is correct neither.

Sorry in advance as I think it's a very noob question, but I'm having trouble googling this one out.

Code: Select all

wxString test = "(";
    if(WxID[0].GetValue() == test[0].GetValue()){
Compiles but doesn't seem to work.
Manolo
Can't get richer than this
Can't get richer than this
Posts: 828
Joined: Mon Apr 30, 2012 11:07 pm

Re: wxString spliting syntax

Post by Manolo »

Code: Select all

mwxString[0].GetValue() == "("
This is wrong. You're trying to compare a string (that inside "") with a wxUniCharRef (returned by GetValue()). More at https://docs.wxwidgets.org/trunk/classwx_string.html "Traps for the unwary"

Use ' instead of "

Code: Select all

mwxString[0].GetValue() == '(' 
While your code is "everything on my own" way, you can simplify your live and use wxString::BeforeFirst see https://docs.wxwidgets.org/trunk/classw ... fc7c0efe70
paddle
Knows some wx things
Knows some wx things
Posts: 43
Joined: Sat Oct 12, 2019 4:57 pm

Re: wxString spliting syntax

Post by paddle »

Thanks!
'(' is working.

Thanks for the reference to wxString::BeforeFirst. However in my case I have in fact recursive structure like ((wxstr1:wxstr2):wxstr3) so just taking the first colon will not work.

What about how to get part of a string? This definitly doesn't work :

Code: Select all

                wxString WxString1 = mwxString[1,colonIndex-1];
                wxString WxString2 = mwxString[colonIndex+1,mwxString.size()-1];
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxString spliting syntax

Post by doublemax »

While the pattern is so simple that you could solve this with dedicated code, you could also use this as an opportunity to learn about regular expressions :)

Code: Select all

#include "wx/regex.h"

wxRegEx rx("\\((.*?):(.*?)\\)", wxRE_ADVANCED);
if (rx.IsValid())
{
  wxString text = "(abcdef:ghijkl)";
  if (rx.Matches(text))
  {
    wxLogMessage("s1: %s", rx.GetMatch(text, 1) );
    wxLogMessage("s2: %s", rx.GetMatch(text, 2) );
  }
  else
  {
    wxLogMessage("no match");
  }
}
Output:

Code: Select all

19:40:27: s1: abcdef
19:40:27: s2: ghijkl
Use the source, Luke!
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

Re: wxString spliting syntax

Post by Kvaz1r »

There is wxStringTokenizer for splitting.
paddle
Knows some wx things
Knows some wx things
Posts: 43
Joined: Sat Oct 12, 2019 4:57 pm

Re: wxString spliting syntax

Post by paddle »

doublemax wrote: Mon Dec 02, 2019 6:39 pm While the pattern is so simple that you could solve this with dedicated code, you could also use this as an opportunity to learn about regular expressions :)

Code: Select all

#include "wx/regex.h"

wxRegEx rx("\\((.*?):(.*?)\\)", wxRE_ADVANCED);
if (rx.IsValid())
{
  wxString text = "(abcdef:ghijkl)";
  if (rx.Matches(text))
  {
    wxLogMessage("s1: %s", rx.GetMatch(text, 1) );
    wxLogMessage("s2: %s", rx.GetMatch(text, 2) );
  }
  else
  {
    wxLogMessage("no match");
  }
}
Output:

Code: Select all

19:40:27: s1: abcdef
19:40:27: s2: ghijkl
That seemed to do just the trick, but it does not work with nested strings as I'm using. For example :
((abcdef:ghijkl):grezagre)

It does not output

Code: Select all

19:40:27: s1: (abcdef:ghijkl)
19:40:27: s2: grezagre
as I would like. Because after I need to recursively call the function on the new substring (abcdef:ghijkl)

So is there a simple way to do substrings like :

Code: Select all

wxString WxString1 = mwxString.substr(a, b);

to get a sub-string from index a to b?

For example
wxstr1 ="0123456789"
wxstr2 = wxstr1.substr(3,8);

wxstr2 : "345678"
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxString spliting syntax

Post by doublemax »

That seemed to do just the trick, but it does not work with nested strings as I'm using. For example :
((abcdef:ghijkl):grezagre)
Well, you didn't describe what input values are possible.
to get a sub-string from index a to b?
https://docs.wxwidgets.org/trunk/classw ... 98fcf151bc
Use the source, Luke!
paddle
Knows some wx things
Knows some wx things
Posts: 43
Joined: Sat Oct 12, 2019 4:57 pm

Re: wxString spliting syntax

Post by paddle »

Thanks! Working as expected :)
Post Reply