locate differences

Prev Next

Function Names

locate differences

Description

This function checks two strings for deviating contents and returns the starting positions and lengths of the different strings. In detail, the function scans the two strings character by character from left to right until a position with different characters have been identified. The same will be repeated with scanning from the end of both strings leftward until a first difference is encountered. The middle contents are deemed different, even if only the first and last character(s) are different, as in follwoing example:

Example 1: Checking: "The yak is blond" and "The jak is blind".Deltas are: "yak is blo" and "jak is bli".
Example 2: Checking: "The yak is blond" and "The jak is blond".Deltas are: "y" and "j".
Options are available to include whole words or numbers in the comparison, so "My name is Jan". / "My name was Dan." will preserve the name.

Call as: function

Restrictions

Indirect parameter passing is disabled

Parameter count

2-6

Parameters

No.TypeDescription
1
input
string 1st string

1st string to compare with 2nd string

2
input
string 2nd string

2nd string to compare with 1st string

Opt. 3
input
numeral Starting Character Position

First character position in both strings where the deviation is beginning.

Opt. 4
output
numeral Length of deviation in 1st string

Number of deviating chracters in the different part of the 1st string

Opt. 5
output
numeral Length of deviation in 2nd string

Number of deviating chracters in the different part of the 2nd string

Opt. 6
input
string Options

Following options apply:

characters Compare character by character.
words If difference in both strings are inside words, then the whole words will be returned.
numbers If difference in both strings are inside numbers, then whole numbers will be returned (minus signs are considered as part of number, but deciaml points are not)
spaces Similar to words, where any contents between spaces are part of the words, including numbers and punctuation symbols.

Default value: characters

Return value

TypeDescription
numeral Comparison result

0 = The two strings are equal
1 = Differences identified.
2 = Differences identified where one string is empty If the option is words, numbers or spaces, then the following values may be returned, too:
3 = Differences identified, and the different string section has been extended to cover whoel words, numbers or non-blank text depending on option chosen.

Examples


               define procedure( printout )
               {
                       echo("Deviation starting position: ", b[], "  Deviation lengths - 1st string: '",
                       c[], "',  2nd string: '", d[], "'   Result: ", r[] );
               }

               r[] = locate differences(Good taste of coffee counts, Good taste of coffee counts, b[], c[], d[] ); // 0, 0,
               printout; // 0, 0, 0, 0

               r[] = locate differences(Good taste of coffee counts, Good taste of caffè counts, b[], c[], d[] );
               printout; // 15, 5 (offee) , 4 (affè) , 1

               r[] = locate differences(Good taste of coffee counts, Good taste of caffè counts, b[], c[], d[], words );
               printout; // 14, 6 (coffee), 5 (caffè), 3

               r[] = locate differences(Good taste of coffee counts, Good taste counts, b[], c[], d[], words );
               printout; // 11, 10 ('of coffee '), 0, 2

               r[] = locate differences(Good tasting of coffee counts, Good taste of caffè counts, b[], c[], d[], words );
               printout; // 5, 17 (tasting of coffee), 14 (taste of caffè) , 3

               r[] = locate differences(Year 2015 begins, Year 2016 begins, b[], c[], d[], characters);
               printout; // 8, 1 ('5'), 1 ('6') , 3

               r[] = locate differences(Year 2015 begins, Year 2016 begins, b[], c[], d[], words);
               printout; // 8, 1 ('5'), 1 ('6') , 3

               r[] = locate differences(Year 2015 begins, Year 2016 begins, b[], c[], d[], numbers);
               printout; // 5, 4 ('2015'), 4 ('2016') , 3

               r[] = locate differences( "20-Mar-2015", "21-Mar-2015" , b[], c[], d[], characters);
               printout; // 1, 1 ('0'), 1 ('1'), 1

               r[]  = locate differences( "20-Mar-2015", "21-Mar-2015" ,b[],  c[], d[], spaces);
               printout; // 0, 11 (20-Mar-2015), 11 (21-Mar-2015), 3

Output

Deviation starting position: 0  Deviation lengths - 1st string: '0',  2nd string: '0'   Result: 0
Deviation starting position: 15  Deviation lengths - 1st string: '5',  2nd string: '4'   Result: 1
Deviation starting position: 14  Deviation lengths - 1st string: '6',  2nd string: '5'   Result: 3
Deviation starting position: 11  Deviation lengths - 1st string: '10',  2nd string: '0'   Result: 2
Deviation starting position: 5  Deviation lengths - 1st string: '17',  2nd string: '14'   Result: 3
Deviation starting position: 8  Deviation lengths - 1st string: '1',  2nd string: '1'   Result: 1
Deviation starting position: 8  Deviation lengths - 1st string: '1',  2nd string: '1'   Result: 1
Deviation starting position: 5  Deviation lengths - 1st string: '4',  2nd string: '4'   Result: 3
Deviation starting position: 1  Deviation lengths - 1st string: '1',  2nd string: '1'   Result: 1
Deviation starting position: 0  Deviation lengths - 1st string: '11',  2nd string: '11'   Result: 3
Try it yourself: Open LIB_Function_locate_differences.b4p in B4P_Examples.zip. Decompress before use.

See also

get differences