Library Elements Basics_String Functions

This article familiarizes the user with the different Basics_String Library elements. All library elements can be found in the Libraries pad of Soley Studio in the category String.

Basics_String_format

When to use

if you want to insert elements of an array in a string and obtain the format of a string at the end.

Syntax

Basics_String_format(FORMAT,CONTENT):RESULT

Input Parameters

FORMAT:string – The first input parameter is the string where you wish to insert the new elements from the array into, with the index of those elements in the array, written between placeholders, in the position where you want them to appear.
CONTENT:array – array of objects that contains the elements to be inserted.

Returns

RESULT:string –  the initial string with the elements of the array, inserted in the specified positions. The end result is also a string.

Example

rule format_demo{ 
  modify{
     eval{ 
       def ref objs:array<object> = array<object>[]; 
       objs.add((object) "Steve"); 
       objs.add((object) 20.4); 
       def var actualString:string = Basics_String_format("Hello {0}, today's temperature is {1}°C. \n\n", objs); 
       emit(actualString + "\n"); 
    }
  }
}

First we created an array of objects (objs)  with two elements: “Steve” and 20.4. We want to insert those two elements in a string. We defined a string variable (actualString), that would contain the value of the return parameter of the Basics_string_format function.

The return paramater would be in our case the following string: “Hello Steve, today’s temeperature is 20.4°C.”

Basics_String_isNullOrEmpty

When to use

This function returns true if a string-variable is empty and false otherwise. If you want to check if a string is empty and use a simple if condition of the style ==””, it can cause an error message if you are not operating on a previously saved data graph, as empty strings are originall yhandled as Null values.

Bad example code for checking empty string properties:

 	if{n.name == ""; }	//if the name is empty

In order to circumvent the aforementioned problem, we recommend to always use the library element Basics_String_isNullorEmpty if you want to check for empty strings.

Syntax

Basics_String_isNullOrEmpty(VALUE):RESULT

Input Parameters

VALUE: string variable to be checked.

Returns

RESULT:boolean – true if null or empty, false otherwise.

Example

rule isNullOrEmpty_demo{ 
  modify{ 
    eval{ 
      def var STR:string = ""; 
      emit("Is STR null or empty?: " + Basics_String_isNullOrEmpty(STR) + "\n"); 
    }
  }
}

The return value is true because STR is an empty string.

Basics_String_toDateTime

When to use

This function converts a string to a double according to a specified DateTime format. If successful the returned double will show the day count and time of day fraction since 12:00 a.m. midnight on 30 December 1899.

Syntax

Basics_String_toDateTime(VALUE,FORMAT):RESULT

Input Parameters

VALUE:string – DateTime value formatted as a string.
FORMAT:string – string that represents that specific DateTime format.

Returns

RESULT:double – a converted DateTime value.

Example

rule toDateTime_demo { 
  modify{ 
    eval{ 
      emit(Basics_String_toDateTime("01.01.2000 18:00:00", "dd.MM.yyyy HH:mm:ss") + "\n"); 
    }
  }
}

The result is the double: 36526.75 and it represents the day count and the time of day fraction between 12:00 a.m. midnight on 30 December 1899 and 6 p.m. on 1 January 2000.

Basics_String_toInt

When to use

If a string is exclusively made up of numerals (-/+ signs at the beginning of the string are also tolerated), then this function would return a boolean variable with the value true and an integer made up of those numerals. Else the function would return false and 0 as integer.

Syntax

Basics_String_toInt(STR):SUCCESS,INT_VALUE

Input Parameters

STR:string – a string made up of numerals.

Returns

SUCCESS:boolean – indicator whether an integer value could be recovered from the input STR.
INT_VALUE:int – integer value corresponding to the input STR.

Example

rule toInt_demo{ 
   modify{ 
      eval{ 
        def var STR:string = "-42"; 
        def var INT_VALUE:int; 
        def var SUCCESS:boolean; 
        (SUCCESS, INT_VALUE) = Basics_String_toInt(STR); 
        emit(SUCCESS + " : " + INT_VALUE + "\n"); 
     }
   }
}

This example will transcribe the value of the given STR(-42) to a variable of type integer. In our case the string STR is made up of numerals beginning with a – sign, so an integer could be made from the string. The SUCCESS variable will then have the value true and the INT_VALUE the value -42.

Basics_String_trim

When to use

This function removes all leading and trailing whitespace characters e.g. space, newline and tab from the input string.

Syntax

Basics_String_trim(STR):RESULT

Input Parameters

STR:string – the input string.

Returns

RESULT:string – string with non-whitespaces as its first and last character.

Example

rule trim_demo{ 
  modify{
     eval{ 
         def var wsString:string = " Hello World "; 
         emit(wsString + "=>" + Basics_String_trim(wsString) + "\n"); 
     }
  }
}

The rule trims the string wsString in such a way that the spaces before and after “Hello Word” will be removed.

Was this article helpful?

Related Articles