Page 1 of 1

char in string

Posted: Sat Apr 12, 2025 1:22 pm
by rndrei
How to read a symbol from a line?

Code: Select all

define txt.s="Hello world!"
define _str.s
define i.b

for i=1 to len(txt)
_str=_str+txt[i]"
next i
debug (_str)
This is sample code, not worked!

Re: char in string

Posted: Sat Apr 12, 2025 1:28 pm
by miso

Code: Select all

Define txt.s="Hello world!"
Define _str.s

For i=1 To Len(txt)
_str=_str+Mid(txt,i,1)
Next i
Debug (_str)

Re: char in string

Posted: Sat Apr 12, 2025 1:34 pm
by rndrei
Got it, thanks!

Re: char in string

Posted: Sat Apr 12, 2025 2:01 pm
by BarryG

Code: Select all

For i=1 To Len(txt)
Reminder: Using Len() like above to parse a string can take a long time if the string is long. It's better to save the value of Len() in a variable and use that instead, like below, so that Len() isn't re-calculated over and over with every loop iteration.

Code: Select all

Define txt.s="Hello world!"
Define _str.s
Define _strlen

_strlen=Len(txt)

For i=1 To _strlen
_str=_str+Mid(txt,i,1)
Next i

Debug (_str)

Re: char in string

Posted: Sat Apr 12, 2025 2:40 pm
by AZJIO
link

Code: Select all

Define txt.s="Hello world!"
Define *c.Character = @txt
Define _str.s

While *c\c
	_str + Chr(*c\c)
	*c + 2
Wend

Debug _str

Code: Select all

Define txt.s="Hello world!"
Define *c.Character = @txt
Define _str.s = Space(Len(txt))
Define *d.Character = @_str

While *c\c
	*d\c= *c\c
	*c + 2
	*d + 2
Wend

Debug _str

Re: char in string

Posted: Sat Apr 12, 2025 3:24 pm
by mk-soft
Many roads lead to Rome ...

Code: Select all

Structure ArrayOfChar
  c.c[0]
EndStructure

Define txt.s="Hello world!"
Define *txt.ArrayOfChar = @txt
Define _str.s

len = Len(txt) - 1
For i = 0 To len
_str =_str + Chr(*txt\c[i])
Next i

Debug (_str)

Re: char in string

Posted: Sat Apr 12, 2025 3:26 pm
by miso
Did not compare, but probably the fastest (AZJIO's version). Though the user must be confident with pointers to use this, or one mistake and won't find where the problem lies, why the app crashes.

mk-soft was faster than me.

Re: char in string

Posted: Sat Apr 12, 2025 4:24 pm
by wilbert
Small variation to the code of mk-soft

Code: Select all

Structure ArrayOfChar
  c.s{1}[0]
EndStructure

Define txt.s="Hello world!"
Define *txt.ArrayOfChar = @txt
Define _str.s

len = Len(txt) - 1
For i = 0 To len
_str =_str + *txt\c[i]
Next i

Debug (_str)

Re: char in string

Posted: Sat Apr 12, 2025 6:19 pm
by mk-soft
Then rather as string signs "s.s{1}[0]", because the types .a, .c, .u are numeric

Code: Select all

Structure ArrayOfChar
  StructureUnion
    c.c[0]
    s.s{1}[0]
  EndStructureUnion
EndStructure

Define txt.s="Hello world!"
Define *txt.ArrayOfChar = @txt
Define _str.s

len = Len(txt) - 1
For i = 0 To len
_str =_str + *txt\s[i]
Next i

Debug (_str)

Re: char in string

Posted: Fri Apr 25, 2025 1:53 am
by AZJIO
The Chr() function and s.s{1} work the same way in terms of speed, so I don't see the point in using an additional structure.
Although Chr() in the C-Backend is slower (630 vs 700).

Code: Select all

EnableExplicit
DisableDebugger

Structure CharStr
  StructureUnion
    c.c
    s.s{1}
  EndStructureUnion
EndStructure

Define *s, StartTime, t1, t2, i, tmp.s
Define *c.CharStr, *u.Character
#Count = 1000000
tmp = "a"
*u = @tmp
*c = @tmp
; *u\c = 55
; *c\c = 55

StartTime = ElapsedMilliseconds()

For i = 0 To #Count
	tmp = Chr(*u\c)
Next

t1 = ElapsedMilliseconds() - StartTime


StartTime = ElapsedMilliseconds()

For i = 0 To #Count
	tmp = *c\s
Next

t2 = ElapsedMilliseconds() - StartTime

EnableDebugger
Debug "Chr = " + Str( t1)
Debug "Struct = " + Str( t2)

Re: char in string

Posted: Fri Apr 25, 2025 7:46 am
by Piero
Testing these 3 methods, I get "changing, variable" results (Mac M1, optimize code)

Code: Select all

Structure ArrayOfChar
   StructureUnion ; shares memory (address)
      c.c[0]
      s.s{1}[0]
   EndStructureUnion
EndStructure

Define txt$="Hello world!"
; will end in 30 seconds on M1 (without debugger):
For i = 0 To 111111 : txt$+"!" : Next 

Define *txt.ArrayOfChar = @txt$
Define *txt2.Character = @txt$
Define _str.s

len = Len(txt$) - 1
t = ElapsedMilliseconds()
For i = 0 To len
   _str + *txt\s[i]
Next i
t1= ElapsedMilliseconds() - t
;Debug (_str)
_str="" ; RESET

;len = Len(txt$) - 1
t = ElapsedMilliseconds()
For i = 0 To len
   _str + Chr(*txt2\c)
   *txt2 + 2 ; SizeOf(Character)
Next i
t2= ElapsedMilliseconds() - t
;Debug (_str)
_str="" ; RESET

;len = Len(txt$) - 1
t = ElapsedMilliseconds()
For i = 0 To len
   _str + Chr(*txt\c[i])
Next i
t3= ElapsedMilliseconds() - t
;Debug (_str)

MessageRequester("Results",Str(t1)+~"\n"+Str(t2)+~"\n"+Str(t3))

Re: char in string

Posted: Fri Apr 25, 2025 7:52 am
by normeus
This might be faster to get the required final output:

Code: Select all

Define txt.s="Hello world!"
Define _str.s=txt
Debug (_str)



norm.

Re: char in string

Posted: Fri Apr 25, 2025 7:59 am
by Piero
normeus wrote: Fri Apr 25, 2025 7:52 am This might be faster to get the required final output:
Nah; I just make a BIG "Hello world!!!!!!!!!!!!!!!" and then read each char and store it on the end of _str (for speed testing)