top of page
Search

String handling

  • Writer: Ivaylo Fiziev
    Ivaylo Fiziev
  • 5 days ago
  • 2 min read
ree

When working with strings, often there is a need of doing string processing. e.g. searching, replacing, extracting substrings. SCL offers a standard set of functions for cases like these. Until now these functions were not supported but since version 2512 they are supported.

With this post I would like showcase the usage of these functions so you have an example. Don't worry. It is simple.



Lets start with a list of them:


  1. LEN - Returns the current length of a string

  2. FIND - Provides the position of a string within another string

  3. LEFT - Provides the first L characters of a string

  4. RIGHT - Provides the last L characters of a string

  5. MID - Provides the middle part of a string

  6. INSERT - Inserts a string, after a specified character index, into another string

  7. DELETE - Deletes several characters, starting from the character at a given position, in a string

  8. REPLACE - Replaces several characters of a string, starting from the character at a given position, with another string

  9. CONCAT - Concatenates two STRING variables together to form one string


Pretty basic stuff but essential when strings are involved.

Lets see how these functions can be used to parse an email address. Imagine you have to extract the user and the domain from it. Here is how it looks:

FUNCTION_BLOCK "MAIN"
VERSION:1.0
VAR_INPUT
	email : STRING := 'user@domain.com';
END_VAR
VAR_OUTPUT
	user : STRING;
	domain : STRING;
END_VAR
VAR_TEMP
	atPos : INT;
	len : INT;
END_VAR
BEGIN
  #len := LEN(#email);
  #atPos := FIND(IN1:=#email, IN2:='@');
  if #atPos > 0 then
  		#user := LEFT(IN:=#email, L:=#atPos - 1);
  		#domain := RIGHT(IN:=#email, L:=#len - #atPos);
  else
  		#user := '';
  		#domain := '';
  end_if;
END_FUNCTION_BLOCK

It works nicely.

The problem is that you cannot wire STRING signals to the input and the outputs. Process Simulate does not support them yet. But imagine if it did and you can do this on a string coming from a PLC or from another source ... it gives you the power to process strings at runtime and respond accordingly. One day it will happen. Until then strings are only for internal use.


Bye!


 
 
 

ความคิดเห็น

ได้รับ 0 เต็ม 5 ดาว
ยังไม่มีการให้คะแนน

ให้คะแนน

Subscribe Form

Thanks for submitting!

bottom of page