Help! #PB_String_NoCase with FindString stopped working

Just starting out? Need help? Post your questions and find answers here.
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Help! #PB_String_NoCase with FindString stopped working

Post by yrreti »

The following FindString #PB_String_NoCasecode always worked before, but it stopped working in this latest version?
I really need this fixed, as I use it a lot in some of my code. I now have to revert back to using LCase or UCase for each FindString.

Code: Select all

;; ----------------------------------------------------
; last modified: September 9th, 2020 @ 2:54:35 PM
; ----------------------------------------------------
                  ;using pb version PureBasic 5.72 (Windows - x86)
                  dat$="Brent Hauble"
                  
                  p1=FindString(dat$,"Hauble",#PB_String_NoCase)
                  Debug "p1 = "+p1
                  If FindString(dat$,"Hauble",#PB_String_NoCase)>0  
                    check=1 ;works because it's a match
                  EndIf
                  
                  p2=FindString(dat$,"HAUBLE",#PB_String_NoCase)
                  Debug "p2 = "+p2
                  If FindString(dat$,"HAUBLE",#PB_String_NoCase)>0  ;case insensitive search (A=a).
                    check=2  ;#PB_String_NoCase  is not working. Why ?  It was working in the last PB version
                  EndIf 
                  Debug "check = "+check                     ;check = 1
                  
                  
                  dat$="Brent HAUBLE"
                  If FindString(dat$,"Hauble",#PB_String_NoCase)>0  
                    check=3 ;#PB_String_NoCase  is not working. Why ?  It was working in the last PB version
                  EndIf
                  If FindString(dat$,"HAUBLE",#PB_String_NoCase)>0  ;case insensitive search (A=a).
                    check=4  ;works because it's a match
                  EndIf 
                  Debug "check = "+check                     ;check = 4
                  
                  ;I now have to revert back to using LCase or UCase
                  Srchdat$="Hauble"
                  Dat2Find$=LCase(Srchdat$)
                  If FindString(LCase(dat$),Dat2Find$)>0
                    check=5
                  EndIf
                  Debug "check = "+check                     ;check = 5
BarryG
Addict
Addict
Posts: 4178
Joined: Thu Apr 18, 2019 8:17 am

Re: Help! #PB_String_NoCase with FindString stopped working

Post by BarryG »

Without even reading your code, I bet it's the missing "StartPosition" flag parameter of FindString() - a classic mistake. Am I right?
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Help! #PB_String_NoCase with FindString stopped working

Post by yrreti »

Start position [, StartPosition [, Mode]]) is optional and is only useful if you want to do your search after a certain string position.
For example in the code: p1 and p2 use the same code except for the string to find
p1 displays 7 showing StartPosition is not needed.
In the case of p2. #PB_String_NoCase used to work fine before, and would have also displayed 7
I've used it for several years with no problem, until I started to have a bug with a recent program I wrote,
and found out that now that switch stopped working.
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2139
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Help! #PB_String_NoCase with FindString stopped working

Post by Andre »

BarryG is right.

If you use for example

Code: Select all

                  p1=FindString(dat$,"Hauble",#PB_String_NoCase)
then the 'mode' constant is used as 3rd parameter.
But the manual clearly states, that the 'mode' parameter must be given at 4th position.
(If using optional parameters all previous parameters must always be given.)

If it worked before, it was just fortune ;-)
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Help! #PB_String_NoCase with FindString stopped working

Post by yrreti »

Thank you BarryG and Andre for your help on this.
I guess I have to chuckle on this. I have used the StartPosition successfully in the past to eliminate searching beginning string
sections that don't need to be searched, but I guess I always thought that the default start position was the first position by
default, because it always worked.

I tried it again on the code that I had posted earlier, and the parts that didn't work, now work. Yah Yah

It's interesting that if the search string and it's case is the same as found in the string to search, you apparently
may not need the StartPosition , as it finds it every time. It's probably when the cases don't match, which need the
#PB_String_NoCase switch, that needs the StartPosition, as you stated to work correctly.
Why it seemed to always work successfully for me in the past for even for unmatch cases too, I don't know.
But I'm glad this problem finally showed up. It would probably have caused me great headaches later.
But just the same, it works! So as it is a good practice moving forward, I will be adding a bunch of StartPosition = 1 in
my existing code, and in any of my new code going forward.

Thank you very much BarryG and Andre for helping me with understanding this problem.
You both saved me a major ton of work.

Sincerely
yrreti
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Help! #PB_String_NoCase with FindString stopped working

Post by Demivec »

yrreti wrote:It's interesting that if the search string and it's case is the same as found in the string to search, you apparently
may not need the StartPosition , as it finds it every time.

Code: Select all

Debug #PB_String_CaseSensitive
Debug  #PB_String_NoCase   
Debug "------------"

;If the 'Start Position' parameter is left out and the 'Mode' parameter is specified, the value
; of the constants will be used for the Start Position.  The constants values are 0 and 1.
;A Start Position of less than 1 will be treated as 1 and the search will take place from the
;beginning of the string, the same as the default. Each of the following examples also uses
;the default search Mode of #PB_String_CaseSensitive (because it isn't specified properly).
Debug FindString("LONG OR SHORT", "SHORT", #PB_String_NoCase)
Debug FindString("LONG OR SHORT", "SHORT", #PB_String_CaseSensitive)
Debug FindString("LONG OR SHORT", "SHORT", -125) 
Debug "------------"

;The mis-specification of the parameters is only detectable when the cases of the source
;string and search string are different.
Debug FindString("LONG OR SHORT", "short", #PB_String_NoCase)
Debug FindString("LONG OR SHORT", "short", #PB_String_CaseSensitive)
Debug "------------"

;As mentioned previously this is the correct way to specify the 'Mode' parameter by include a Start Position.
Debug FindString("LONG OR SHORT", "short", 1, #PB_String_NoCase)
Debug FindString("LONG OR short", "short", 1, #PB_String_CaseSensitive)
Outputs:

Code: Select all

0
1
------------
9
9
9
------------
0
0
------------
9
9
Post Reply