Perform string operations

<< Click to Display Table of Contents >>

Navigation:  Bizagi Studio > Process wizard > Business Rules > Business rules examples >

Perform string operations

This section provide examples on how to perform the most frequently used string operations:

 

Concatenate strings

Convert letters in capital letters

Create a sub-string of a text

Calculate the length of the string

Convert a number to string

 

Concatenate Strings

An automated process may need to assign a value to an attribute that contains a concatenation result of two or more strings.

 

Suppose  the first Task of the Purchase Request Process (Create Purchase Request), references three attributes (First Name, Last Name and Full Name). The Full Name attribute will be read-only, and will contain the result of concatenating the First Name and Last Name of Purchase Requester.

 

 

StringOperations1

 

 

1.In the fourth step of the Bizagi Process Wizard, select Activity Actions.

Click the Task where this action will be performed, and create an Expression.

 

StringOperations2

 

2. Type the Name, Display Name and Description of the expression.

 

 

StringOperations3

 
3. In the Expressions Editor, add the variables FirstString and SecondString that are to be used in the expression as indicated below.

 

StringOperations4

 

 

4. Create an expression to concatenate First Name and Last Name, and assign the result to the FullName variable.

The concatenation is easily done by using the addition (+) operator as shown in the image below.

 

StringOperations5

 

//Assign attributes to variables

FirstString=<PurchaseRequest.Requester.FirstName>;

SecondString=<PurchaseRequest.Requester.LastName>;

 

//Assign concatenated text to the target attribute

<PurchaseRequest.Requester.FullName>=FirstString+" "+ SecondString;

 

Click Ok.

 

Once the expression is associated with the process, the user can test the functionality. In the images below, the three Attributes are displayed before, and after the expression is executed. Once the expression performs the concatenation, it can be evidenced in the Full Name control.

 

StringOperations6

 

StringOperations7

 

 

Convert letters to Capital letters

For this example, you have to perform the same steps explained for the Concatenation example, in order to create and associate the expression with the Purchase Request process. However, in this example, the next modifications need to be performed in the expression:

 

1. Create the following variables for your expression.

 

StringOperations8

 

2. Apply the .ToUpper method to the concatenated string, and assign the result to the target attribute.

 

StringOperations10

 

//Assign attributes values to variables

FirstString=<PurchaseRequest.Requester.FirstName>;

SecondString=<PurchaseRequest.Requester.LastName>;

 

//Build concatenated text for the target attribute

ConcatenatedString=FirstString+" "+ SecondString;

 

//Convert to Capital letters

UpperString=ConcatenatedString.ToUpper();

//Assign concatenated text to the target attribute

<PurchaseRequest.Requester.FullName>=UpperString;

 

Once this is done, the next step is to test the changes to the expression in the Work Portal. In this example, this is done in the first activity of the Purchase Request process. The result is shown in the images below:

 

StringOperations6

 

StringOperations9

 

Note the expression is associated with an On Save event. Once you click Save in the corresponding Task, the expression will execute, and the Full Name field will be valued with the concatenation result of the First Name and Last Name, presented in capital letters.

 

Create a sub-string of a text

The Substring function allows you to obtain a subset of the symbols in a string, where the order of the elements is preserved.

 

Suppose we wish to obtain the initials of the Requester. For this example, you have to perform the same steps explained for the Concatenate Strings example, in order to create and associate the expression with the Purchase Request process. However, in this example, the next modifications need to be performed in the expression:

 

1. Create the following variables:

 

StringOperations11

 

2. Include the Substring sentence into the expression.

 

The Substring method has the following syntax:

 

String.Substring(position of initial character, number of characters);

 

 

StringOperations12

 

 

Substring returns the specified number of characters given the position of the initial character. For instance, if you apply a substring to the text "Bizagi" with parameters 2,2 (String.Substring(2, 2)) you will obtain "za".

 

 

StringOperations13

 

 

The next image shows the expression to obtain the initials of the Purchase Requester.

 

StringOperations14

 

//Assign attribute values to variables

FirstString=<PurchaseRequest.Requester.FirstName>;

SecondString=<PurchaseRequest.Requester.LastName>;

 

//Build concatenated text calling the Substring method

Initials=FirstString.Substring(0,1)+" "+ SecondString.Substring(0,1);

 

//Assign concatenated result to the target attribute

<PurchaseRequest.Requester.FullName>=Initials;

 

Once this is done, the next step is to test the changes to the expression in the Work Portal. In this example, this is done in the first activity of the Purchase Request process. The result is shown in the images below:

 

 

StringOperations6

 

 

StringOperations15

 

The expression is associated with an Event On Save. Once the user presses the Save button, the expression will be executed. The Full Name field will be updated with the initials of the First Name and the Last Name.

 

 

Calculate the length of the string

Suppose we wish to validate that the concatenated string we obtained in the previous example of this section has, as maximum, 10 characters. If the concatenated string has more than 10 characters a substring must be applied to resize it to the required length.

 

To obtain the length of a string we use the .Length method which returns an integer value:

String.Length;

 

For our example it will be used to establish the condition of the validation as follows.

 

StringOperations17

 

//Assign attribute values to variables

FirstString=<PurchaseRequest.Requester.FirstName>;

SecondString=<PurchaseRequest.Requester.LastName>;

 

//Build concatenated text

ConcatenatedString=FirstString+" "+ SecondString;

 

if (ConcatenatedString.length<10)

{

//Assign concatenated text to the target attribute

<PurchaseRequest.Requester.FullName>=ConcatenatedString;

} else

{

//Apply substring

ConcatenatedString=ConcatenatedString.Substring(0,10);

<PurchaseRequest.Requester.FullName>=ConcatenatedString;

}

 

Once this is done, the next step is to test the changes to the expression in the Work Portal. In this example, this is done in the first activity of the Purchase Request process. Insert a First Name and Last Name with a total number of characters greater than 10. The result is a truncated full name with only 10 characters (including the space between First and Last name), as shown in the images below:

 

StringOperations6

 

StringOperations18

 

 

Convert a number to string

Suppose that in a Loan Request process the passport number of customers is stored in an Integer type attribute. This number is used as parameter in a web service to consult credit blacklists, however the exposed services receives a string type parameter so a type conversion is needed from integer to string.

 

To convert the passport number into a string we use the .toString method:

Number.toString;

 

StringOperations19

 

 

//Obtain the number to be converted

var PassportNum= <CreditRequest.Customer.PassportNumber>;

//Convert to string and assign

<CreditRequest.Customer.StrPassportNumber> = PassportNum.toString();