Page 1 of 1
wxString spliting syntax
Posted: Mon Dec 02, 2019 3:30 pm
by paddle
I have a wxString formated as follow :
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.
Re: wxString spliting syntax
Posted: Mon Dec 02, 2019 4:18 pm
by Manolo
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
"
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
Re: wxString spliting syntax
Posted: Mon Dec 02, 2019 4:30 pm
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];
Re: wxString spliting syntax
Posted: Mon Dec 02, 2019 6:39 pm
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
Re: wxString spliting syntax
Posted: Mon Dec 02, 2019 7:05 pm
by Kvaz1r
There is
wxStringTokenizer for splitting.
Re: wxString spliting syntax
Posted: Tue Dec 03, 2019 12:47 pm
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"
Re: wxString spliting syntax
Posted: Tue Dec 03, 2019 1:17 pm
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
Re: wxString spliting syntax
Posted: Tue Dec 03, 2019 1:39 pm
by paddle
Thanks! Working as expected
