Search string

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

Search string

Post 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
User avatar
HeX0R
Addict
Addict
Posts: 1218
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Search string

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

Re: Search string

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

Re: Search string

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

Re: Search string

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

Re: Search string

Post by rndrei »

Thank you! Everything works!
Post Reply