get differences

Prev Next

Function Names

get differences

Description

This function checks two strings for deviating contents and copies them in two substrings. 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-5

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
output
string Deviating part of 1st string

Identified part of the 1st string which differs from the part of the 2nd string

Opt. 4
output
string Deviating part of 2nd string

Identified part of the 1st string which differs from the part of the 2nd string

Opt. 5
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


      r[] = get differences(Good taste of coffee counts, Good taste of coffee counts, c[], d[] );
      echo("1st string: '", c[], "',    2nd string: '", d[], "'   Result: ", r[] ); // '', '', 0 (equal)

      r[] = get differences(Good taste of coffee counts, Good taste of caffè counts, c[], d[] );
      echo("1st string: '", c[], "',    2nd string: '", d[], "'   Result: ", r[] ); // offee, affè, 1

      r[] = get differences(Good taste of coffee counts, Good taste of caffè counts, c[], d[], words );
      echo("1st string: '", c[], "',    2nd string: '", d[], "'   Result: ", r[] ); // coffee, caffè, 3

      r[] = get differences(Good taste of coffee counts, Good taste counts, c[], d[], words );
      echo("1st string: '", c[], "',    2nd string: '", d[], "'   Result: ", r[] ); // coffee, '', 2

      r[] = get differences(Good tasting of coffee counts, Good taste of caffè counts, c[], d[], words );
      echo("1st string: '", c[], "',    2nd string: '", d[], "'   Result: ", r[] ); // tasting of coffee, taste of caffè, 3

      r[] = get differences(Year 2015 begins, Year 2016 begins, c[], d[], characters);
      echo("1st string: '", c[], "',    2nd string: '", d[], "'   Result: ", r[] ); // 5, 6, 1

      r[] = get differences(Year 2015 begins, Year 2016 begins, c[], d[], words);
      echo("1st string: '", c[], "',    2nd string: '", d[], "'   Result: ", r[] ); // 5, 6, 1

      r[] = get differences(Year 2015 begins, Year 2016 begins, c[], d[], numbers);
      echo("1st string: '", c[], "',    2nd string: '", d[], "'   Result: ", r[] ); // 2015, 2016, 3

      r[] = get differences( "20-Mar-2015", "21-Mar-2015" , c[], d[], characters);
      echo("1st string: '", c[], "',    2nd string: '", d[], "'   Result: ", r[] ); // 0, 1, 1

      r[]  = get differences( "20-Mar-2015", "21-Mar-2015" , c[], d[], spaces);
      echo("1st string: '", c[], "',    2nd string: '", d[], "'   Result: ", r[] ); // 20-Mar-2015, 20-Mar-2016, 1

Output

1st string: '',    2nd string: ''   Result: 0
1st string: 'offee',    2nd string: 'affè'   Result: 1
1st string: 'coffee',    2nd string: 'caffè'   Result: 3
1st string: 'of coffee ',    2nd string: ''   Result: 2
1st string: 'tasting of coffee',    2nd string: 'taste of caffè'   Result: 3
1st string: '5',    2nd string: '6'   Result: 1
1st string: '5',    2nd string: '6'   Result: 1
1st string: '2015',    2nd string: '2016'   Result: 3
1st string: '0',    2nd string: '1'   Result: 1
1st string: '20-Mar-2015',    2nd string: '21-Mar-2015'   Result: 3
Try it yourself: Open LIB_Function_get_differences.b4p in B4P_Examples.zip. Decompress before use.

See also

locate differences