<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Is there a string compare function in ST ?</title><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/d10da40b33/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/d10da40b33/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/CODESYS-V2/thread/d10da40b33/</id><updated>2020-02-27T21:04:34.542000Z</updated><subtitle>Recent posts to Is there a string compare function in ST ?</subtitle><entry><title>Is there a string compare function in ST ?</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/d10da40b33/?limit=25#4c40" rel="alternate"/><published>2020-02-27T21:04:34.542000Z</published><updated>2020-02-27T21:04:34.542000Z</updated><author><name>ViggoTW</name><uri>https://forge.codesys.com/u/viggotw/</uri></author><id>https://forge.codesys.com2df5e6595f02b597cb840ac560bc3948f6df3d58</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi,&lt;br/&gt;
I realize this is an old post, but I stumbled upon this thread while searching a solution to the same problem.&lt;br/&gt;
As STRCMP is not included in the standard library, I guess this would still be relevant information for some.&lt;/p&gt;
&lt;p&gt;Would it not be possible to solve this challenge using pointers, checking one character at the time?&lt;br/&gt;
Here is an example, called by &lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="c"&gt;(*** Declaration ***)&lt;/span&gt;
&lt;span class="n"&gt;FUNCTION&lt;/span&gt; &lt;span class="n"&gt;STRCMP&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;BOOL&lt;/span&gt;
&lt;span class="n"&gt;VAR_INPUT&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;ptStr1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;POINTER&lt;/span&gt; &lt;span class="n"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;BYTE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;ptStr2&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;POINTER&lt;/span&gt; &lt;span class="n"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;BYTE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;END_VAR&lt;/span&gt;
&lt;span class="c"&gt;(*** Code***)&lt;/span&gt;
&lt;span class="n"&gt;WHILE&lt;/span&gt; &lt;span class="n"&gt;TRUE&lt;/span&gt; &lt;span class="n"&gt;DO&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;IF&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptStr1&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ptStr2&lt;/span&gt;&lt;span class="o"&gt;^)&lt;/span&gt; &lt;span class="n"&gt;THEN&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="c"&gt;(* If two characters are unequal, return FALSE *)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;STRCMP&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;FALSE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;RETURN&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;ELSIF&lt;/span&gt; &lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;ptStr1&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;AND&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptStr2&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="n"&gt;THEN&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="c"&gt;(* If both strings have reached the NULL TERMINATOR at the same place, the strings are identical *)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;STRCMP&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;TRUE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;RETURN&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;ELSE&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="c"&gt;(* If the two characters were equal, but were not NULL TERMINATORS, increment to check next character *)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;ptStr1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;ptStr1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;ptStr2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;ptStr2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;END_IF&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="c"&gt;(* Make sure the pointer doesn't wander into unknown territory and create problems *)&lt;/span&gt;
&lt;span class="n"&gt;END_WHILE&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.  &lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Is there a string compare function in ST ?</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/d10da40b33/?limit=25#fff7" rel="alternate"/><published>2020-02-27T21:04:33.066000Z</published><updated>2020-02-27T21:04:33.066000Z</updated><author><name>DaveW</name><uri>https://forge.codesys.com/u/davew/</uri></author><id>https://forge.codesys.com5c2b61bc8034b88a9b486e6a41856cd8ea7ea597</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Is there a string compare function in ST ?</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/d10da40b33/?limit=25#1535" rel="alternate"/><published>2020-02-27T21:04:28.806000Z</published><updated>2020-02-27T21:04:28.806000Z</updated><author><name>JAPIB</name><uri>https://forge.codesys.com/u/japib/</uri></author><id>https://forge.codesys.comdb5b4f09be9e6534c4c576d1f7adb48d4f8b780e</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Look at system libraries. It depends on the currently used target system.&lt;br/&gt;
There is a library named &lt;strong&gt;SysLibStr.lib&lt;/strong&gt;, in where you will find :&lt;br/&gt;
&lt;strong&gt;SysStrCmp&lt;/strong&gt;&lt;br/&gt;
This function of type DINT compares lexicographically two strings and returns one of the following values:&lt;br/&gt;
Return value &amp;lt; 0 String1 smaller than String2&lt;br/&gt;
Return value = 0 String1 = String2&lt;br/&gt;
Return value &amp;gt; 0 String1 bigger als String2&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;SysStrCmpI&lt;/strong&gt;&lt;br/&gt;
This function of type DINT checks whether two strings are identical and returns one of the following values:&lt;br/&gt;
Return value &amp;lt; 0 String1 smaller than String2&lt;br/&gt;
Return value = 0 String1 = String2&lt;br/&gt;
Return value &amp;gt; 0 String1 bigger than String2&lt;/p&gt;
&lt;p&gt;Hope this suits you  &lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Is there a string compare function in ST ?</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/d10da40b33/?limit=25#38a8" rel="alternate"/><published>2020-02-27T21:04:27.276000Z</published><updated>2020-02-27T21:04:27.276000Z</updated><author><name>DaveW</name><uri>https://forge.codesys.com/u/davew/</uri></author><id>https://forge.codesys.come4d76bf39987629d16a2b3c6e8986c06fddd5087</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;Any suggestions welcome&lt;/p&gt;
&lt;p&gt;DaveW&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>