Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find n-th occurrence of delimiter in string
#1
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.
#2
Finally, I succeeded with the following code, including in the case of unicode characters. Any better solution is still welcome :

Macro Macro4
Code:
Copy      Help
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}"
#3
Macro Macro2607
Code:
Copy      Help
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)
#4
This is perfect. Gintaras, many thanks indeed !
#5
But it must be valid single-row CSV. For example, error if
Macro Macro2607
Code:
Copy      Help
str s="''Description;A;100;;;Y;Help;;;400;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"
or incorrect result if
Macro Macro2607
Code:
Copy      Help
str s="Descr[]iption;A;100;;;Y;Help;;;400;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"
#6
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.


Forum Jump:


Users browsing this thread: 1 Guest(s)