Solved

Search for a string in a string

Posted on 1997-11-30
3
227 Views
Last Modified: 2010-04-04
Hi,
I have a button and a TEdit control on a form.
The user can put words separated by spaces into the
TEdit. I have strings in an ini file. Lets say the
ini is called nov.ini and the section is called [img1] and the identity is called 'description'. I need to take all the words from the TEdit and see if any of them are in the string or part there of and do one thing if yes and not do anything if no.
The ini looks like this:

[img1]
Description=this is image number one.

so if the user puts in "image" or "image one" or "th" or any part of this string, it returns a yes.

Thank you in advance,
Max
0
Comment
Question by:maxb
3 Comments
 
LVL 1

Expert Comment

by:anilms
ID: 1352461
You require two things - a tokenizer to get all the tokens which were input and a parser to parse the string. Here are two functions for that :

Tokenizing :
{This function returns the next token in a string passed to it and removes the token from the string. When there are no more tokens left, it returns a null ie, ''}

function get_token(var xstr:string):string;
var tmpstr:string;
    i:integer;
begin
     tmpstr:='';
     if length(xstr)>0 do
     begin
          i:=1;
          {Adding the characters which make up the token}
          while xstr[i]<>' ' do
                tmpstr := tmpstr+xstr[i];
          {Space seperates tokens, dont worry if there is more
          than one space}

          {removing the token from the origina string - so  
          always pass a copy of the string and not the original
          string}
          xstr := copy(xstr,i+1,length(xstr)-i);
     end;
     {Getting rid of leading and trailing blank spaces}
     get_token:=trim(tmpstr);
end;

Parsing :
Delphi has a built in parsing function - pos. This checks for the existance of a string within a string and returns the position. Example :
x := pos('Disk','Hard Disk')
 gives the result 6 as Disk is found at location 6
y:= pos('Man','What is happening here?')
 gives the result 0 as it is not able to find the substring 'Man' in the string.
0
 

Author Comment

by:maxb
ID: 1352462
I'm sory..I just dont get how this token thing works or how to call it or anything. Pretend I have a putton on a form and a TEdit1 as well as a string from nov.ini in the section [img1] and subsection "description". Would you please tell me how to implement that step by stem into the tokenizing thing cause that seems to be above my level of understanding. Thank you.
Max B
0
 
LVL 3

Accepted Solution

by:
altena earned 100 total points
ID: 1352463
Ok Max, I'll try to give some sample code. But please exuse
any typo's since I am sitting at a PC without delphi on it.

Here we go:

To improve readability the programming style will not be perfect.
I've put comments at the bad spots.

--------------------
   TEdit1.text = trim(TEdit1.text);  //remove head/tail spaces.
   SpacePos = pos(TEdit1.text, ' '); //Find first space.

   While SpacePos <> 0 do   //loop over all words
      begin

      //Trim TEdit1.text
      TEdit1.text = trim(TEdit1.text); //Double code is bad.

      NextWord = Copy(TEdit1.text, 1, Spacepos);
       
      //Remove this word from TEdit1.text.
      TEdit1.text = Copy(TEdit1.text,
         Spacepos + 1,
         10240);       //todo: Calculate Length correctly.
       //This code fails if there is more than 10K in Tedit1.

      //todo: Check for off by 1 errors (is the space included?)
      //    make sure that Nextword does NOT contain any spaces.
      // This is were you are out of luck, if I had a copy of
      // Delphi available I'd check it for you. Sorry

      //Check against the ini string
      If pos(NextWord, IniString) <> 0 then YES //found it.
                                       else NO  //didn't find it

     //go to the next word:
      SpacePos = pos(TEdit1.text, ' ');  //Double code is bad.

      end;

 //Once you get here you will have to check the last word
 // Or TEdit1.Text was empty.
--------------------

Again: This is not the best looping technique for this problem
but I hope you find it a readable one.

Here is how the loop should be done (IMHO)
while true do begin
   ...
   break out of the loop when finished;  
   ...
   end;

The sample code I entered will break down under maintenance.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A short film showing how OnPage and Connectwise integration works.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now