October 30, 2025, 12:45:04 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Finding out if a string is present

Started by Brian, May 03, 2013, 12:26:16 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Brian

May 03, 2013, 12:26:16 PM Last Edit: May 03, 2013, 02:12:42 PM by Brian Pugh
Hi,

I know I'm going to beat myself up with this one, but can't find the answer:

How do I know if a certain sequence of characters, say "KC1500" is within a longer string?
I just want to know if the sequence is there, before I decide what to do with the whole string

Thanks,

Brian

Edit: Yep, I've just beat myself up! Found out how to do it

Bill-Bo

May 03, 2013, 04:18:54 PM #1 Last Edit: May 03, 2013, 04:22:37 PM by Bill-Bo
Brian,

I can't whip anything up in 10 minutes like the old days, but
I'd use MID$ and single-step through the string until I found
KC1500, and indicate a yea or nay. I've done it before. I'll
see what I can do.

LarryMac'll probably beat me to it.

Bill

LarryMc

Quote from: Bill-Bo on May 03, 2013, 04:18:54 PM
Brian,

I can't whip anything up in 10 minutes like the old days, but
I'd use MID$ and single-step through the string until I found
KC1500, and indicate a yea or nay. I've done it before. I'll
see what I can do.

LarryMac'll probably beat me to it.

Bill
Bill, he figured it out for himself; thats why I never posted.

The way to do it is:
if INSTR(text$,"KC1500")
  ' found it
else
  'not there
endif

A little more elaborate and flexible
find_str = "KC1500"
if INSTR(LCASE$(text$),LCASE$(find_str))
  ' found it
else
  'not there
endif

The 2nd version doesn't care where find_str gets its value from.
Also, since the INSTR function is case sensitive the LCASE$ functions take care of that.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Bill-Bo

May 03, 2013, 07:20:58 PM #3 Last Edit: May 03, 2013, 07:35:54 PM by Bill-Bo
LarryMack and Brian,

I've spent the last 45 minutes trying to work up something.
I did it in the new 2.5 beta. Heck, it should work in most
anything. But look at it.

Now, I didn't get fancy; just a Yes or No, no option for
another word, etc.

DEF Sentence$:string                                            ' The string you want to look at,
DEF wordlookingfor$:string                                      ' and the word you're looking for.
def w:int
Sentence$="This is a model KC1500."
INPUT "Enter the word you want: ",wordlookingfor$               ' This is case sensitive
  FOR w = 1 to LEN(Sentence$) - LEN(wordlookingfor$) + 1        ' Make a loop to check for the characters in wordlookingfor$
    IF wordlookingfor$ = MID$(Sentence$,w,LEN(wordlookingfor$)) ' If the word is there,
       PRINT "Yes"                                              ' say so.
         ELSE                                                   ' Else
          NEXT w                                                ' keep checking,
       PRINT "No"                                               ' and if the word is not there, say so.
     END IF                                                     ' end when done
PRINT "Press Any Key To Close"
DO:UNTIL INKEY$ <> ""


I haven't used the "Insert Code" before. Hope it works.

Brian, I was just giving you an idea.

BTW, my youngest son's name is Brian.

LarryMac, I'm going to take a closer look at you two bits of code.

It's console, because I don't know how to do the windows (GUI's),
but I know you all do.

Bill

LarryMc

Bill
The code as you posted won't work.
You have your 'NEXT w" statement in the wrong place.
You have it between your else and endif statements
If you move the NEXT w to right before the
PRINT "Press Any Key To Close"statement it will at least work, sorta

With that code structure it will print 17 "No" and 1 "Yes"

Using the INSTR function you get this, with no looping, and only one Yes/No.
   DEF Sentence$:string                                         ' The string you want to look at,
   DEF wordlookingfor$:string                                       ' and the word you're looking for.
   def w:int
   Sentence$="This is a model KC1500."
   INPUT "Enter the word you want: ",wordlookingfor$      ' This is case sensitive
   IF INSTR(Sentence$,wordlookingfor$)                         ' If the word is there,
      PRINT "Yes"                                                         ' say so.
   ELSE                                                                      ' Else
      PRINT "No"                                                          ' and if the word is not there, say so.
   END IF
   PRINT "Press Any Key To Close"
   DO:UNTIL INKEY$ <> ""
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Bill-Bo

LarryMac,

I've attached the exe of what you say will not work.
It works for me. I had to make the extension iwb
because we can't attach exe files. Just change the
extension.

Run it and type KC1500 and you get yes.
Run it and type model and you get yes.
Run it and type xxx and you get no.

Today I am going to work with your way.

Thanks

Bill

aurelCB

Bill
You do this in a wrong way ,how you mean to change extension ?
Better if you wont that anyone look your code pack your exe with zip
and then add it as attachment.
I don't see any problem if you do this on that way... ;)

LarryMc

Quote from: Bill-Bo on May 04, 2013, 06:45:54 AM
LarryMac,

I've attached the exe of what you say will not work.
It works for me.
In the simple one pass program you posted it will work
If you put it in a program and repeatedly call it it'll work until you possibly run out of stack space because you are creating a  FOR/NEXT loop but never exiting it properly when it is a YES
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Bill-Bo

LarryMac,

Okay. If I had it where you could look for another word,
I would have ran into that. Thanks.

Aurel,

I made the extension iwb for the attachment, because
it's the first time I've attached anything on this site. I
had first tried an exe, but it wouldn't let me, and I didn't
notice zip in the list it gave, so I used iwb.

Thanks, again,

Bill