how is a string terminated ?

Just starting out? Need help? Post your questions and find answers here.
ludoke
Enthusiast
Enthusiast
Posts: 155
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

how is a string terminated ?

Post by ludoke »

How is a string in an array of strings terminated ?
Do I have to add a certain character myself #CR$ ? this works .
Do I have to determine the string length first?
What is the best way ?

Code: Select all

Dim text.s(100)  ;array of string
l.i  ;length string
text(1)="text1"+ #CR$ ;add an extra #CR$
text(2)="This is text2"
l=Len(text(2))*2  ;length of string
*t=@text(2)
While n=< l                            ;this work ,but need length of string
  kar.a=PeekA(*t+n)
  Debug Chr(kar)
  n=n+2
Wend  
;------------------------------------
n=0   ;reset n
*t=@text(1)         
Repeat
   kar.a=PeekA(*t+n)
   Debug Chr(kar)
   n=n+2
Until kar=#CR     ;
debug kar             ;read 13 ,it works
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: how is a string terminated ?

Post by mk-soft »

An array of string is an array of pointer to string. A string variable is always the pointer to the string. Thus, the strings in the array are necessarily one after the other in the memory.
If there is not enough space for the new string in the array, a new memory is requested and the old one is released.

Code: Select all

Dim text.s(10)

text(0) = "Hello"
text(1) = "World"

*pArray = @text()
*text1 = PeekI(*pArray)
*text2 = PeekI(*pArray + SizeOf(integer))

Debug "Pointer 1 = " + Hex(*text1) + " String = " + PeekS(*text1)
Debug "Pointer 2 = " + Hex(*text2) + " String = " + PeekS(*text2)

text(0) = "Hello .......... xxxxxxxxxxxxxxxxxxxx "

*text1 = PeekI(*pArray)
*text2 = PeekI(*pArray + SizeOf(integer))

Debug "Pointer 1 = " + Hex(*text1) + " String = " + PeekS(*text1)
Debug "Pointer 2 = " + Hex(*text2) + " String = " + PeekS(*text2)
Last edited by mk-soft on Fri Jan 01, 2021 9:37 pm, edited 1 time in total.
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
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: how is a string terminated ?

Post by skywalk »

They are terminated with two 0 bytes for unicode compilation.

Code: Select all

Structure s1
  s$[3]
EndStructure
Dim s$(2)
Define s.s1
For i = 0 To 2
  s$(i)   = Str(i) + "A"
  s\s$[i] = Str(i) + "S"
Next i
ShowMemoryViewer(@s$(0),255)
CallDebugger
ShowMemoryViewer(@s\s$,255)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: how is a string terminated ?

Post by mk-soft »

Forgot to mention. How skywalk

Don't forget!
You cannot directly add any characters to the string in memory. As shown above, if there is not enough space, a new memory is allocated for the string.
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
ludoke
Enthusiast
Enthusiast
Posts: 155
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

Re: how is a string terminated ?

Post by ludoke »

thanks,

my problem is that I want to read a string from an array, character by character.

Depending on the character I have to do something.
I can first determine the length with LEN (string), but that is more time.

I can add a #CR when creating the strings in the array, in the example I added it later, but it turns out to work.
But maybe it doesn't always work as MK-soft says.

at Skywalk,
I found in C, strings are terminated with a null character (ASCII code 0).Is that what you mean ?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: how is a string terminated ?

Post by RASHAD »

Hi

Code: Select all

Dim st.s(1)
st(0) = "Hellow World"
For i = 0 To StringByteLength(st(0)) Step 2
  Debug Chr(PeekA(@st(0)+i))
Next
Egypt my love
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: how is a string terminated ?

Post by Saki »

Well, with strings it's important that you understand exactly how it works, it's easy to make mistakes.
For example, you can also create a string from spaces, i.e. an empty one, which is then automatically terminated at the end.
In this string you can insert a #LF$ at any desired position or PokeW two zeros to terminate it.
If your strings never exceed a certain maximum you can pre define and cut them arbitrarily.
A small overhead does not matter.
If you use strings in PB you will always have an overhead, which is much bigger, but not disturbing.
The problem is when you destroy the termination at the end.
Then PB searches for the next occurring two zeros in memory.
You always have to look at what you need.
地球上の平和
ludoke
Enthusiast
Enthusiast
Posts: 155
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

Re: how is a string terminated ?

Post by ludoke »

Rashad,

Dim st.s(1)
st(0) = "Hellow World"
n=Len(st(0))
Debug "len= "+n ; gives 12
For i = 0 To 39 Step 2
Debug Chr(PeekA(@st(0)+i))
Next


the length of the string =12 ,but 2 bytes/char=24 ?
It works with 22 but also with 24 till 39 ,then a strange character comes.

You need the length of the string,or you must have a special character at the end of the string to indicate the end.
I'm still trying to learn a lot.

As Saki,explain there are some extra places ,but I will add a #LF$ or #CR$ to find the end of the string .
Thanks to all .
Cezary
Enthusiast
Enthusiast
Posts: 108
Joined: Sun Feb 12, 2017 2:31 pm

Re: how is a string terminated ?

Post by Cezary »

Maybe the safest (but slower) method would be not to use pointers?

Code: Select all

Dim st.s(1)
st(1) = "Hellow World"
For i = 0 To 14
  Debug Mid(st(1), i, 1) + " " + Asc(Mid(st(1), i, 1))
Next
In addition, the end of a string can be detected as long as there are no characters with code zero in it.
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: how is a string terminated ?

Post by mk-soft »

Also under Purebasic, the strings are terminated with a NULL byte (word unicode mode).
The data type char under PB is a word in unicode mode.

You can change individual characters, but you cannot add more characters.

Example over pointers like 'C'

Code: Select all

;-TOP

Structure ArrayOfChar
  c.c[0] ; The value NULL defines an array of unknown size.
EndStructure

Dim text.s(1)
text(0) = "Hello World!"

Define *pText.ArrayOfChar = @text(0)
Define index = 0

Repeat
  If *pText\c[index] = 0
    ;TODO
    Break
  EndIf
  
  Debug "Index " + index + " Char: " + *pText\c[index]
  
  Select *pText\c[index]
    Case ' '
      *pText\c[index] = '.'
      
  EndSelect
  
  index + 1
ForEver

Debug text(0)
Last edited by mk-soft on Sat Jan 02, 2021 12:55 pm, edited 2 times in total.
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
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: how is a string terminated ?

Post by Saki »

Code: Select all

Dim st.s(1)
st(0) = "Hello World1234"
n=Len(st(0))
Debug "len= "+n
Repeat
  ii=PeekW(@st(0)+i)
  Debug Chr(ii)
  i+2
Until Not ii
地球上の平和
User avatar
NicTheQuick
Addict
Addict
Posts: 1503
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: how is a string terminated ?

Post by NicTheQuick »

This is how I usually iterate over strings:

Code: Select all

EnableExplicit

Define text.s = "Hello World"

Define *c.Character = @text

While *c\c
	Debug *c\c
	*c + SizeOf(Character)
Wend
The while-loop automatically stops if the null-characters was reached.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: how is a string terminated ?

Post by Saki »

Nick, Yeah, more professional....
Perfect !

I would have done it the same way, but I didn't want to confuse him, because the question was very simple.
地球上の平和
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: how is a string terminated ?

Post by mk-soft »

Mine is not professional. cry :cry: :mrgreen:
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
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: how is a string terminated ?

Post by Saki »

Sorry, your is also professional, how ever ! 8)
I had looked at it all only for a minute as I'm still working here....

But I must also confess that I often use the peek, poke methods out of sheer laziness as well :cry:
Last edited by Saki on Sat Jan 02, 2021 1:41 pm, edited 1 time in total.
地球上の平和
Post Reply