Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Is there a string compare function in ST ?

DaveW
2011-08-03
2017-11-08
  • DaveW - 2011-08-03

    Hi,

    I am trying to sort some strings in alphabetical order, I can't find string compare functions. If I write my own in ST how do get the characters out of the string? BYTE is a data type which would be the same size as a STRING character, but I don't think I can read the char. In C it would be c = str[x]; but then C also has string compare functions.

    Any suggestions welcome

    DaveW

     
  • JAPIB

    JAPIB - 2011-08-03

    Look at system libraries. It depends on the currently used target system.
    There is a library named SysLibStr.lib, in where you will find :
    SysStrCmp
    This function of type DINT compares lexicographically two strings and returns one of the following values:
    Return value < 0 String1 smaller than String2
    Return value = 0 String1 = String2
    Return value > 0 String1 bigger als String2

    SysStrCmpI
    This function of type DINT checks whether two strings are identical and returns one of the following values:
    Return value < 0 String1 smaller than String2
    Return value = 0 String1 = String2
    Return value > 0 String1 bigger than String2

    Hope this suits you

     
  • DaveW - 2011-08-04

    Thanks for the information. I just checked and could not find this lib on my system CoDeSys V3.4 SP3 patch 2 controller is from Bivator.

    However there is the OSCAT basic lib which has a CODE function that gives the byte value of a char in a string so maybe that will be my starting point.

     
  • ViggoTW - 2017-11-08

    Hi,
    I realize this is an old post, but I stumbled upon this thread while searching a solution to the same problem.
    As STRCMP is not included in the standard library, I guess this would still be relevant information for some.

    Would it not be possible to solve this challenge using pointers, checking one character at the time?
    Here is an example, called by

    (*** Declaration ***)
    FUNCTION STRCMP : BOOL
    VAR_INPUT
       ptStr1 : POINTER TO BYTE;
       ptStr2 : POINTER TO BYTE;
    END_VAR
    (*** Code***)
    WHILE TRUE DO
       IF (ptStr1^ <> ptStr2^) THEN            (* If two characters are unequal, return FALSE *)
          STRCMP := FALSE;
          RETURN;
       ELSIF ((ptStr1^ = 0) AND (ptStr2^ = 0)) THEN   (* If both strings have reached the NULL TERMINATOR at the same place, the strings are identical *)
          STRCMP := TRUE;
          RETURN;
       ELSE                           (* If the two characters were equal, but were not NULL TERMINATORS, increment to check next character *)
          ptStr1 := ptStr1 + 1;
          ptStr2 := ptStr2 + 1;
       END_IF
                                  (* Make sure the pointer doesn't wander into unknown territory and create problems *)
    END_WHILE
    

    Of course, you should be careful with having a pointer wandering around in your memory in a "WHILE TRUE"-loop, so an improvement could be to limit the loop to the length of the shortest input string.

    EDIT: After thinking about it; it should technically not be an issue "wandering around" in the memory this way since you are only reading, not writing. It is of course not encouraged nonetheless.

     

Log in to post a comment.