Posts: 1,058
Threads: 367
Joined: Oct 2007
I need to locate the substring between the 7-th to eighth occurrence of a delimiter (namely semi-colon) in the following string.
Quote:str s="Description;A;100;;;Y;Help;;;400;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"
Any advice is mostly welcome.
Let me add that I used tok but it misses any "empty" occurrence between to successive delimiters. I also tried findrx
Quote:findrx(subject pattern 0 4 a)
with cpMin, cpMax, but it fails in the case that the string contains unicode characters.
Many thanks in advance.
Posts: 1,058
Threads: 367
Joined: Oct 2007
Finally, I succeeded with the following code, including in the case of unicode characters. Any better solution is still welcome :
Macro
Macro4
subject="Perigrafi;A;100;;;Y;Help;;;400;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"
str pattern=";"
str s
int i; ARRAY(CHARRANGE) a
findrx(subject pattern 0 4 a)
s.get(subject 29 43-29)
out s
for i 0 a.len-1
,int i0=a[0 i].cpMax
,int i1=a[0 i+1].cpMin
,s.get(subject i0 i1-i0)
,out F"{i} {i0} {i1} {s}"
Posts: 12,135
Threads: 142
Joined: Dec 2002
Macro
Macro2607
str s="Description;A;100;;;Y;Help;;;400;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"
ICsv x._create; x.Separator=";"
x.FromString(s)
int i
for i 0 x.ColumnCount
,out x.Cell(0 i)
Posts: 1,058
Threads: 367
Joined: Oct 2007
This is perfect. Gintaras, many thanks indeed !
Posts: 12,135
Threads: 142
Joined: Dec 2002
But it must be valid single-row CSV. For example, error if
Macro
Macro2607
str s="''Description;A;100;;;Y;Help;;;400;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"
or incorrect result if
Macro Macro2607
str s="Descr[]iption;A;100;;;Y;Help;;;400;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"
Posts: 1,058
Threads: 367
Joined: Oct 2007
Thank you for this explanation. By the way in the case of my inquiry it was actually a valid single-row CSV, but I forgot to mention it. Nevertheless, the code I proposed above it works in any case.