Solved

Search for a string in a string

Posted on 1997-11-30
3
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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