Solved

Search for a string in a string

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

695 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