char in string

Just starting out? Need help? Post your questions and find answers here.
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 151
Joined: Thu Dec 28, 2023 9:04 pm

char in string

Post 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!
miso
Enthusiast
Enthusiast
Posts: 410
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: char in string

Post 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)
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 151
Joined: Thu Dec 28, 2023 9:04 pm

Re: char in string

Post by rndrei »

Got it, thanks!
BarryG
Addict
Addict
Posts: 4123
Joined: Thu Apr 18, 2019 8:17 am

Re: char in string

Post 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)
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: char in string

Post 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
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: char in string

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
miso
Enthusiast
Enthusiast
Posts: 410
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: char in string

Post 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.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: char in string

Post 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)
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: char in string

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: char in string

Post 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)
User avatar
Piero
Addict
Addict
Posts: 863
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: char in string

Post 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))
Last edited by Piero on Fri Apr 25, 2025 8:04 am, edited 1 time in total.
normeus
Enthusiast
Enthusiast
Posts: 470
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: char in string

Post 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.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
Piero
Addict
Addict
Posts: 863
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: char in string

Post 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)
Post Reply