New functions to work with strings

Released in 1C:Enterprise 8.3.6.
We have extended a set of functions designed to work with strings. We have done this in order to give you more advanced tools to parse string data. New functions will be convenient and useful in technological tasks of text analyzing. In the tasks associated with text parsing which that contains data in a formatted form. It can be the analysis of some files received from hardware or, for example, the analysis of technical journal.

All actions performed by new functions you could perform before using more or less complex algorithms written in the script. Therefore, the new functions do not give you some fundamentally new abilities. However, they allow reducing an amount of code, making the code more simple and easy-to-understand. In addition, they can speed up the execution of actions. Because the functions implemented in the platform operate, of course, faster than the same algorithm written in the script.

Formatting function StrTemplate()

This function inserts the parameters in the string. The need of such transformation often appears, for example, when you display the warning messages. The syntax of this function is as follows:
StrTemplate(<Template>, <Value1-Value10>)
<Template> – a string in which the representations of parameters must be substituted.
<Value1> , … <Value10> – the parameters (maximum – ten) the representations of which must be substituted in the string.

To indicate a specific place in the template in which the substitution must be performed, you need to use the markers like %1, … %10. A number of markers used in the template and a number of parameters that contain the values should match.
For example, a result of the execution of such operator:

Result = StrTemplate("Error in string %1 (type %2 is required)", 2, "Date")

Will be a string:
“Error in string 2 (type Date is required)”

Function to work with strings StrCompare()

This function compares to strings in case-insensitive mode. For example:

Result = StrCompare("First string", "Second string")

The same action you could execute using an object CompareValues:

CompareValues = New CompareValues;
Result = CompareValues.Compare("First string", "Second string")

However, the use of new function looks more simple. In addition, the function, unlike the object CompareValues, operates both in thin client and web-client.

Functions to work with strings StrStartWith(), StrEndsWith()

These functions determine whether the string starts with a specified substring, whether the string ends with a specified substring. The algorithm of these functions is not difficult to implement in the script, but their presence allows you to write more pure and understandable code. And they operate faster.
For example, it is convenient to use them in the operator If:

If StrStartWith(URL, "http://") Then
    //external link
ElseIf StrStartWith(URL, "e1c://") Then
    //internal link
Functions to work with strings StrSplit(), StrConcat()

These functions separate the string into the parts by the specified delimiter. Or vice versa, concatenate several strings in one string inserting a selected delimiter between them. They are useful to create and analyze the logs, technological journal. For example, you can easily separate the log of technological journal into the parts suitable for further analysis:

ArrayString = StrSplit("42:10.863015-1,DBMSSQL,5,process=rphost,p:processName=trade,t:clientID=77,t:applicationName=1CV8C,t:computerName=SERVER1C,
|t:connectID=6,SessionID=19,Usr=Johnson,AppID=1CV8C,Trans=0,dbpid=52,Sql='SELECT T1._IDRRef, T1._IDRRef, T1._Description, T1._Type FROM dbo._Chrc94 
|T1 WITH(NOLOCK) WHERE (T1._ParentIDRRef = ?) p_0:xB5F406243FA37DFE48D2460721BCEC84',Rows=1,RowsAffected=-1", ",")
Function to work with strings StrFind()

Instead of old function Find(), we have implemented a new function which has additional features:

  • Search in different directions (from the beginning, from the end);
  • Search from the specified position;
  • Search of the entry with specified number (the second, the third, etc.).

In fact, it duplicates the features of old function. It is done in order to maintain the compatibility with modules compiled in the old versions. It is recommended not to use the old function Find().

There is an example below which uses the new search features. The reverse search is useful when you need the last fragment of formalized string, for example, a full file name in URL. And the search from specified position can be very useful when you need to search in a certain fragment, not in the entire string.

DelimName = StrFind("", "/", SearchDirection.FromEnd);
Ext = StrFind("", ".",, DelimName );
[Total: 0    Average: 0/5]

Leave a Reply

Your email address will not be published. Required fields are marked *