Strncmp Function

Action

Compares lexicographically and case-sensitively the first count characters in two strings. For case-insensitive string comparison refer to the Strnicmp function.

Include file

Kernel.bdh

Syntax

Strncmp(in sString1 : string,in sString2 : string,in nCount : number): number;

Return value

  • value < 0. sString1 is less than sString2

  • value = 0. sString1 is identical to sString2

  • value > 0. sString1 is greater than sString2

Parameter Description
sString1 Defines the first string to compare
sString2 Defines the second string to compare
nCount Defines the number of characters to compare

Example

dcltrans
  transaction TStrncmp
  var
    sString1, sString2: string;
  begin
    sString1 := "Hello world!";
    sString2 := "Hello WORLD!";
    write("string1 = "); write(sString1); writeln;
    write("string2 = "); write(sString2); writeln;
    // compare strings
    if Strncmp(sString1, sString2, Strlen(sString1)) = 0 then
      write("strings are equal"); writeln;
    else
      write("strings are not equal"); writeln;
    end;
    // compare first 5 characters of strings
    if Strncmp(sString1, sString2, 5) = 0 then
      write("first 5 characters of the strings are equal"); writeln;
    else
      write("first 5 characters are not equal"); writeln;
    end;
  end TStrncmp;

Output

string1 = Hello world!
string2 = Hello WORLD!
strings are not equal
first 5 characters of the strings are equal