Solved

Search for a string in a string

Posted on 1997-11-30
3
229 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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