Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to find whole words within a string Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
14-Nov-02
Category
Algorithm
Language
Delphi 2.x
Views
65
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Stewart Moss

Does anyone know of a function that finds a whole word within a string as in the 
search and replace options?

Answer:
1   
2   {Function FindWord
3   
4   Parameters:
5   pattern: word to search for
6   text: text to search
7   caseSensitive: determines whether search is case sensitive or not. Default is not 
8   case-sensitive.
9   startAt: first character to search, default is 1.
10  
11  Returns:
12  The start of the first instance of the word, or 0, if the word was not found or 
13  only as part of larger words. A word in this context is any sequence of 
14  alphanumeric characters delimited by non-alphanumeric characters.
15  
16  Error Conditions: none
17  
18  Created: 18.05.99 by P. Below}
19  
20  function FindWord(pattern, text: string; caseSensitive: Boolean = false; startAt:
21    Integer = 1): Integer;
22  var
23    offset, endOfPattern: Integer;
24  begin
25    Result := 0;
26    if Length(text) = 0 then
27      exit;
28    if Length(pattern) = 0 then
29    begin
30      {By definition a pattern of length 0 is always found}
31      result := 1;
32      Exit;
33    end;
34    if not caseSensitive then
35    begin
36      pattern := AnsiLowerCase(pattern);
37      text := AnsiLowerCase(text);
38    end;
39    endOfPattern := startAt + Length(pattern);
40    for offset := startAt to Length(text) - Length(pattern) + 1 do
41    begin
42      if pattern[1] = text[offset] then
43      begin
44        if ((offset = 1) or not IsCharAlphaNumeric(text[offset - 1])) and 
45  ((endOfPattern
46          > Length(text)) or  not IsCharAlphaNumeric(text[endOfPattern])) 
47  				and (StrLComp(@text[offset], @pattern[1], Length(pattern)) = 0) then
48        begin
49          Result := offset;
50          exit;
51        end;
52      end;
53      Inc(endOfPattern);
54    end;
55  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC