How can I convert zstring ptr to standard string

New to FreeBASIC? Post your questions here.
objet-a
Posts: 13
Joined: Jul 13, 2021 0:04
Location: Hubei, China

Re: How can I convert zstring ptr to standard string

Post by objet-a »

SARG wrote: Apr 07, 2022 9:29 Split function
delimiter : by default one space
limit by default all the substrings (could be 1, 2,..)

Code: Select all

sub split(res() as string,strg as string,delim as string=" ",limit as integer=-1)
	dim as integer resmax=0
	dim as integer strglen=len(strg),posinstrg=1,delimlen=len(delim),delimfound
	if strglen=0 then ''len = zero --> empty array
		return
	End If
	
	if len(delimlen)=0 or instr(posinstrg,strg,delim)=0 then ''One element the original string
		redim res(0)
		res(0)=strg
		return
	End If
	
	if limit=-1 then
		limit=9999999999
	End If
	
	while posinstrg<strglen and resmax<limit
		
		delimfound=instr(posinstrg,strg,delim)
		redim preserve res(resmax)
		if delimfound then
			res(resmax)=mid(strg,posinstrg,delimfound-posinstrg)
			posinstrg+=delimlen+len(res(resmax))
		else
			res(resmax)=mid(strg,posinstrg)
			posinstrg+=len(res(resmax))
		end if
		resmax+=1
		
	Wend
	return
End sub

redim as string rstrg()
split(rstrg(),"abc - def - fgh"," - ")
for i as integer =lbound(rstrg) to ubound(rstrg)
	print i,rstrg(i)
Next

print "end"
sleep
A little but elegant change:

Code: Select all

#include once "crt/limits.bi"
function split(res() as string,strg as string,delim as string=" ",limit as integer=-1)as integer /' return value is total number of elements'/
	dim as integer resmax=0
	dim as integer strglen=len(strg),posinstrg=1,delimlen=len(delim),delimfound
	if strglen=0 then ''len = zero --> empty array
		return 0
	End If
	
	if len(delimlen)=0 or instr(posinstrg,strg,delim)=0 then ''One element the original string
		redim res(0)
		res(0)=strg
		return 1
	End If
	
	if limit=-1 then
		limit=INT_MAX
	End If
	
	while posinstrg<strglen and resmax<limit
		
		delimfound=instr(posinstrg,strg,delim)
		redim preserve res(resmax)
		if delimfound then
			res(resmax)=mid(strg,posinstrg,delimfound-posinstrg)
			posinstrg+=delimlen+len(res(resmax))
		else
			res(resmax)=mid(strg,posinstrg)
			posinstrg+=len(res(resmax))
		end if
		resmax+=1
	Wend
	
	return resmax
End FUNCTION
markem
Posts: 49
Joined: Aug 30, 2006 23:17
Contact:

Re: How can I convert zstring ptr to standard string

Post by markem »

I realize this is a old posting but here is my two cents worth:

1. ZString is different from a regular string.
2. BUT! In trying to get ARGC and ARGV please note that, in order to get the string out of ARGV you have to use a pointer.
2a. MOST pointers are just A = PTR but in this case you are dealing with a double pointer (ie: A pointer to a pointer). This means that in order to get to the STRING you must use a star in front of the variable (ie: *PTR) and not just a pointer.
3. So, most simply, you just need to do the following:

Code: Select all

   dim myString as STRING
   dim argc as Integer
   dim argv as Zstring PTR PTR

   myString = *argv[0-N] ' In other words - whichever ARGV variable you want - you use that number. Like *argv[0]
Just figured that out and thought it should be posted since this entire list of posts was to show how to do this simple thing.
Post Reply