# Board index » delphi » String within a String

## String within a String

##### Quote
mdsi...@primenet.com (Martin Simmonds) wrote:
>If I have for example a string that contains;

>"The Queen of England is really cool"

>What is the best way to code to search for the word "England" ?

>Martin...

var X : byte;
......
X  :=  Pos(substring, string);

if X = 0 then {no substring was found within string}
else {the substring was found starting at position X in string}

regards Sven

## Re:String within a String

Use the Boyer-Moore algorithm to minimize the number of character
comparisons.  Summary from a data structures book by Stubbs & Webre
follows:

1.  Character comparisons are made starting at the right end of the
pattern and working toward the left.  The important consequence is
that some text characters are never considered.

2.  After a miss, the decision about how far to advance the pattern is
based on two independent considerations:
a.  The value of the text character at which the miss occurs and the
distance to the left at which that value first occurs in the
pattern.
b.  The values of all text characters that have been hit together with
the value that is known not to occur because of a miss.

3.  The pattern is moved the distance given by the larger of 2a and 2b
above.

Boyer-Moore algorithm preprocesses the pattern and constructs two
tables.  The first table records the distance from the right end of the
pattern to the first occurrence of each letter of the alphabet for the
application.  This satisfies 2a.  The second table contains one value for
each character in the pattern.

Place pattern at text[pos];
WHILE pattern NOT matched AND text NOT exhausted DO
WHILE pattern character differs from the text character DO
Shift pattern as far to the right as possible;
Start search at the right end of pattern;
END;
Back up to the prior character of both text and pattern;
END;

##### Quote
On Wed, 9 Aug 1995, Martin Simmonds wrote:
> "The Queen of England is really cool"

> What is the best way to code to search for the word "England" ?

## Re:String within a String

If I have for example a string that contains;

"The Queen of England is really cool"

What is the best way to code to search for the word "England" ?

Martin...

## Re:String within a String

##### Quote
>If I have for example a string that contains;
>"The Queen of England is really cool"
>What is the best way to code to search for the word "England" ?

Simplest, if not best, might be

If Pos('England','The Queen of England is really cool') > 0 Then
{Exists}
Else
{Substring does not exist}

Normally of course the strings would be held in variables !!:-)

HTH
Ray Cramer
R...@Pol.ac.uk

## Re:String within a String

In article <4096lk\$...@nnrp3.primenet.com>, mdsi...@primenet.com (Martin Simmonds) says:

##### Quote

>If I have for example a string that contains;

>"The Queen of England is really cool"

>What is the best way to code to search for the word "England" ?

>Martin...

Try the pos function :

const
mystr : string = 'The Queen of england is really cool';
var x : byte;
begin
if pos('England',mystr) <> 0 then x := pos('England',mystr);
end

after this code, x will point to the first letter of 'England' (assuming
that it exists in the string

the pos function returns 0 if the substring does not exist, and the point
in the string where the substring starts if it does exist.

Nowhere Man