Page 1 of 1

Search string

Posted: Thu Mar 13, 2025 1:26 pm
by rndrei
Why doesn't the code work? Two lines should display:
1. Hello all
2. Thiis is not work

Code: Select all

   
Define string$
Define *buffer
Define data_label.s
Define i.i
Define char_label.c
Define label_text.s

string$=lf$+"Hello all #"+lf$+" this is Not work#"+lf$
      *Buffer = UTF8(string$)
      data_label=PeekS(*buffer,-1,#PB_UTF8)
      Repeat
       Last = Position
       Position = FindString(data_label , "#", Position + 1)
        If Position > 0
          Debug "':' found at position: " + Position
          For i = position To 1 Step -1
            char_label= Asc(Mid(data_label,i, 1))
            If char_label<>#LF Or char_label<>#CR
              label_text=label_text+Chr(char_label)
            ElseIf  char_label=#LF Or char_label=#CR
              label_text=""           
            EndIf
            
           Next i
           Debug "label_text:" + ReverseString(label_text)
           label_text=""
        EndIf
      Until Not Position

Re: Search string

Posted: Thu Mar 13, 2025 1:31 pm
by HeX0R
What would you expect to happen here, when char_label is a #LF

Code: Select all

If char_label<>#LF Or char_label<>#CR

Re: Search string

Posted: Thu Mar 13, 2025 2:27 pm
by AZJIO
HeX0R wrote: Thu Mar 13, 2025 1:31 pm What would you expect to happen here, when char_label is a #LF

Code: Select all

If char_label<>#LF Or char_label<>#CR
Which is the same as char_label = anything

Re: Search string

Posted: Thu Mar 13, 2025 2:41 pm
by rndrei
It turns out that the second line is entirely!

---------------------------------------------------
found at position: 11
label_text:Hello all #
'found at position: 29
label_text:Hello all # this is Not work#-
--------------------------------------------------------

Re: Search string

Posted: Thu Mar 13, 2025 2:52 pm
by AZJIO

Code: Select all

Define string$
Define *buffer
Define data_label.s
Define i.i
Define char_label.c
Define label_text.s

lf$ = #LF$

data_label = lf$ + "Hello all #" + lf$ + " this is Not work#" + lf$
Repeat
; 	label_text = ""
	Position = FindString(data_label , "#", Position + 1)
	If Position > 0
		Debug "> pos: " + Position
		For i = position To 1 Step -1
			char_label = Asc(Mid(data_label, i, 1))
			If char_label = #LF Or char_label = #CR
				Break
			Else
				label_text = label_text + Chr(char_label)
			EndIf
		Next
		
		Debug "txt: " + ReverseString(label_text)
		label_text = ""
	EndIf
Until Not Position
Without creating a problem

Code: Select all

Define string$
Define data_label.s
Define i.i
Define char_label.s
Define label_text.s

lf$ = #LF$

data_label = lf$ + "Hello all #" + lf$ + " this is Not work#" + lf$
Repeat
; 	label_text = ""
	Position = FindString(data_label , "#", Position + 1)
	If Position > 0
		Debug "> pos: " + Position
		For i = position To 1 Step -1
			char_label = Mid(data_label, i, 1)
			If char_label = #LF$ Or char_label = #CR$
				Break
			Else
				label_text = label_text + char_label
			EndIf
		Next
		
		Debug "txt: " + ReverseString(label_text)
		label_text = ""
	EndIf
Until Not Position
A better way

Code: Select all

Define data_label.s
Define i.i
Define *c.Character
Define *b

lf$ = #LF$

data_label = lf$ + "Hello all #" + lf$ + " this is Not work#" + lf$
*c = @data_label
*start = *c
While *c\c
	If *c\c = '#'
; 		Debug Chr(*c\c)
		*b = *c
		For i = *c To *start Step -SizeOf(Character)
			*c - SizeOf(Character)
			If *c\c = #LF Or *c\c = #CR
				Debug PeekS(*c + SizeOf(Character), (*b - *c) / SizeOf(Character))
			EndIf
		Next
		*start = *b + SizeOf(Character)
		*c = *b
	EndIf
	*c + SizeOf(Character)
Wend

Re: Search string

Posted: Thu Mar 13, 2025 3:29 pm
by rndrei
Thank you! Everything works!