Solved

Search for a string in a string

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

10 Experts available now in Live!

Get 1:1 Help Now