There are few mechanisms for working with strings in 1C queries. First, the lines can be added. Secondly, you can take a substring from a string. Thirdly, strings can be compared, including by pattern. That's probably all that can be done with strings.

String addition

To add rows in a query, the “+” operation is used. You can only add strings of limited length.

SELECT "Name: " + Counterparties. Name AS Column 1 FROM Directory. Counterparties AS Counterparties WHERE Counterparties. Link = &Link

Substring function

SUBSTRING(<Строка>, <НачальнаяПозиция>, <Длина>)

An analogue of the Environment() function from the object model. The Substring() function can be applied to string data and allows you to select a fragment <Строки> , starting with the character number <НачальнаяПозиция> (characters in a line are numbered starting from 1) and length <Длина> characters. The result of the function calculation has a variable-length string type, and the length will be considered unlimited if <Строка> has unlimited length and parameter <Длина> is not a constant or greater than 1024.

If the length of the string is less than specified in the second parameter, then the function will return an empty string.

Attention! Using the SUBSTRING() function to convert strings of unlimited length to strings of limited length is not recommended. Instead, it is better to use the cast operator EXPRESS().

Function Similar

If we need to make sure that a string attribute meets certain criteria, we compare it:

SELECT Counterparties. Name AS Column 1 FROM Directory. Counterparties AS Counterparties WHERE Counterparties. Name = "Gazprom"

But what if you need a more subtle comparison? Not just equality or inequality, but similarity to a certain pattern? This is exactly what the SIMILAR function was created for.

LIKE — Operator for checking a string for similarity to a pattern. Analogue of LIKE in SQL.

The SIMILAR operator allows you to compare the value of the expression specified to the left of it with the pattern string specified to the right. The value of the expression must be of type string. If the value of the expression matches the pattern, the result of the operator will be TRUE, otherwise it will be FALSE.

The following characters in the pattern string are service characters and have a meaning different from the string character:

  • % (percent): a sequence containing any number of arbitrary characters;
  • _ (underscore): one arbitrary character;
  • […] (one or more characters in square brackets): any single character listed inside the square brackets. The enumeration may contain ranges, for example a-z, meaning an arbitrary character included in the range, including the ends of the range;
  • [^...] (in square brackets a negation sign followed by one or more characters): any single character other than those listed following the negation sign.

Any other symbol means itself and does not carry any additional load. If one of the listed characters needs to be written as itself, then it must be preceded by<Спецсимвол>. Myself<Спецсимвол>(any suitable character) is defined in the same statement after the SPECIAL CHARACTER keyword.

A string is one of the primitive data types in 1C:Enterprise 8 systems. Variables with the type line contain text.

Type variable values line are enclosed in double quotes. Several variables of this type can be added.

Per1 = "Word 1" ;
Per2 = "Word 2" ;
Per3 = Per1 + " " + Per2;

In the end Per3 will mean " Word 1 Word 2″.

In addition, 1C:Enterprise 8 systems provide functions for working with strings. Let's look at the main ones:

EnterString(<Строка>, <Подсказка>, <Длина>, <Многострочность>) — the function is designed to display a dialog box in which the user can specify the value of a variable of type Line. Parameter <Строка> is required and contains the name of the variable into which the entered string will be written. Parameter <Подсказка> optional - this is the title of the dialog box. Parameter <Длина> optional, shows the maximum length of the input string. Defaults to zero, which means unlimited length. Parameter <Многострочность> optional. Defines the multiline text input mode: True—multiline text input with line separators; False - enter a simple string.

You can enter a string if you know the character code in Unicode:

Symbol(<КодСимвола>) — the code is entered as a number.

Letter= Symbol(1103) ; // I

There is also an inverse function that allows you to find out the code of a symbol.

SymbolCode(<Строка>, <НомерСимвола>) — returns the Unicode number of the specified character as a number.

Text case conversion functions:

VReg(<Строка>) — Converts all characters in a string to uppercase.

NReg(<Строка>) — Converts all characters in a string to lowercase.

TReg(<Строка>) — converts all characters in the string to title case. That is, the first letters in all words are converted to upper case, and the remaining letters are converted to lower case.

Functions for searching and replacing characters in a string:

Find(<Строка>, <ПодстрокаПоиска>) — finds the character number of the occurrence of the search substring. For example:

Find ("String" , "oka" ) ; // 4

StrFind(<Строка>, <ПодстрокаПоиска>, <НаправлениеПоиска>, <НачальнаяПозиция>, <НомерВхождения>) — finds the character number of the occurrence of the search substring, the occurrence number is indicated in the corresponding parameter. In this case, the search begins with the character whose number is specified in the parameter InitialPosition. Searching is possible from the beginning or the end of the string. For example:

Number4 Occurrences = Str Find ( "Defensiveness", "about" ,Search Direction. From Start, 1, 4); // 7

StrReplace(<Строка>, <ПодстрокаПоиска>, <ПодстрокаЗамены>) – finds all occurrences of the search substring in the source string and replaces it with the replacement substring.

StrReplace ("String" , "oka" , "" ) ; // Page

EmptyString(<Строка>) – checks the string for significant characters. If there are no significant characters, or no characters at all, then the value is returned True. Otherwise - Lie.

StrNumberOccurrences(<Строка>, <ПодстрокаПоиска>) – Calculates the number of occurrences of the search substring in the source string.

StrNumberOccurrences ( "learn, study and study again", "study" , "" ) ; // 3

StrTemplate(<Строка>, <ЗначениеПодстановки1>…<ЗначениеПодстановкиN> — substitutes parameters into a string by number. The line must contain substitution markers of the form: “%1..%N”. Marker numbering starts from 1. If the parameter value Undefined, an empty string is substituted.

StrTemplate ( "Parameter 1 = %1, Parameter 2 = %2", "1" , "2" ) ; // Parameter 1= 1, Parameter 2 = 2

String conversion functions:

Lion(<Строка>, <ЧислоСимволов>) – returns the first characters of a string.

Right(<Строка>, <ЧислоСимволов>) – returns the last characters of a string.

Wednesday(<Строка>, <НачальныйНомер>, <ЧислоСимволов>) – returns a string of length<ЧислоСимволов>, starting from symbol<НачальныйНомер>.

AbbrL(<Строка>) trims non-significant characters to the left of the first significant character in the string.

Abbr(<Строка>) — cuts off insignificant characters to the right of the last significant character in the line.

AbbrLP(<Строка>) – cuts off insignificant characters to the left of the first significant character in the line and to the right of the last significant character in the line.

StrGetString(<Строка>, <НомерСтроки>) – Gets a multiline string by number.

Other features:

StrLength(<Строка>) – returns the number of characters in the string.

StrNumberRow(<Строка>) – returns the number of lines in a multiline string. A line is considered new if it is separated from the previous one by a newline character.

StrCompare(<Строка1>, <Строка2> ) – compares two strings in a case-insensitive manner. A function works similar to an object Comparison of Values. Returns:

  • 1 - if the first line is greater than the second
  • -1 - if the second line is greater than the first
  • 0 - if the strings are equal

StrCompare("First line" , "Second line" ) ; // 1

Rows in 1C 8.3 in the built-in 1C language are values ​​of a primitive type Line. Values ​​of this type contain a Unicode string of arbitrary length. String type variables are a set of characters enclosed in quotes.

Example 1. Let's create a string variable with text.

StringVariable = "Hello world!";

Functions for working with strings in 1s 8.3

This section will provide the main functions that allow you to change lines in 1c, or analyze the information contained in them.

StrLength

StrLength(<Строка>) . Returns the number of characters contained in the string passed as a parameter.

Example 2. Let's count the number of characters in the line “Hello world!”

String = "Hello world!"; NumberofCharacters = StrLength(String); Report(NumberCharacters);

The result of executing this code will be the display of the number of characters in the line: 11.

AbbrL

AbbrL(<Строка>) . Trims non-significant characters to the left of the first significant character in the string.
Minor characters:

  • space;
  • non-breaking space;
  • tabulation;
  • carriage return;
  • line feed;
  • translation of the form (page).

Example 3. Remove all spaces from the left side of the line "world!" and add the line “Hello” to it.

String = Abbreviation("world!"); String = "Hello"+String; Report(String);

The result of executing this code will be the display of the line “Hello world!” on the screen.

Abbreviated

Abbr(<Строка>) . Trims non-significant characters to the right of the first significant character in the string.

Example 4. Form from the lines “Hello” and “peace!” the phrase "Hello world!"

Line = Abbreviation("Hello ")+" "+ Abbreviation(" world!"); Report(String);

AbbrLP

AbbrLP(<Строка>) . Trims non-significant characters to the right of the first significant character in the string, and also trims non-significant characters to the left of the first significant character in the string. This function is used more often than the previous two, as it is more universal.

Example 5. Remove insignificant characters on the left and right in the name of the counterparty.

Counterparty = Directories. Counterparties.Find By Details("TIN", "0777121211"); AccountObject = Account.GetObject(); CounterpartyObject.Name = AbbrLP(CounterpartyObject.Name); AccountObject.Write();

Lion

Lion(<Строка>, <ЧислоСимволов>) . Gets the first characters of the string, the number of characters is specified in the parameter Number of Characters.

Example 6. Let in the structure Employee contain the first, last and patronymic names of the employee. Get a string with last name and initials.

NameInitial = Lev(Employee.Name, 1); Patronymic Initial = Leo(Employee. Patronymic, 1); FullName = Employee.LastName + " " + FirstName Initial + "." + Middle Initial + ".";

Right

Right(<Строка>, <ЧислоСимволов>) . Gets the last characters of a string, the number of characters specified in the parameter Number of Characters. If the specified number of characters exceeds the length of the string, then the entire string is returned.

Example 7. Let a date in the format “yyyymmdd” be written at the end of a string variable, get a string with the date and convert it to type Date.

String = "Current date: 20170910"; StringDate = Rights(String, 8); Date = Date(StringDate);

Wednesday

Wednesday(<Строка>, <НачальныйНомер>, <ЧислоСимволов>) . Gets a substring from the string passed as a parameter Line, starting from the character whose number is specified in the parameter InitialNumber and the length passed into the parameter Number of Characters. The numbering of characters in a line starts from 1. If in the parameter InitialNumber a value less than or equal to zero is specified, then the parameter takes the value 1. If the parameter Number of Characters is not specified, then characters up to the end of the line are selected.

Example 8. Let the string variable starting from the ninth position contain the region code, you should get it and write it in a separate line.

String = "Region: 99 Moscow"; Region = Avg(String, 9, 2);

PageFind

StrFind(<Строка>, <ПодстрокаПоиска>, <НаправлениеПоиска>, <НачальнаяПозиция>, <НомерВхождения>) . Searches for a specified substring in a string, returning the position number of the first character of the found substring. Let's look at the parameters of this function:

  • Line. Source string;
  • Search Substring. Search substring;
  • Search Direction. Specifies the direction to search for a substring in a string. Can take values:
    • Search Direction.From Start;
    • Search.End Direction;
  • InitialPosition. Specifies the position in the string at which the search begins;
  • NumberOccurrences. Specifies the occurrence number of the searched substring in the source string.

Example 9. In the line “Hello world!” Determine the position of the last occurrence of the "and" character.

PositionNumber = StrFind("Hello world!", "and", Search Direction.End); Report(PositionNumber);

The result of executing this code will be to display the number of the last occurrence of the “and” symbol: 9.

VReg

VReg(<Строка>) . Converts all characters in the specified string in 1s8 to uppercase.

Example 10: Convert the string "hello world!" to upper case.

StringVreg = VReg("hello world!"); Report(StringVreg);

The result of executing this code will be the display of the line “HELLO WORLD!”

NReg

NReg(<Строка>) . Converts all characters of the specified string in 1s 8 to lower case.

Example 11: Convert the string "HELLO WORLD!" to lower case.

StringNreg = NReg("HELLO WORLD!"); Report(StringVreg);

The result of executing this code will be the display of the line “hello world!”

Treg

TReg(<Строка>) . Converts a string as follows: the first character of each word is converted to upper case, the remaining characters of the word are converted to lower case.

Example 12: Capitalize the first letters of the words in the line “hello world!”

StringTreg = TReg("hello world!"); Report(StringTreg);

The result of executing this code will be the display of the line “Hello World!”

Symbol

Symbol(<КодСимвола>) . Gets a character by its Unicod code.

Example 13. Add left and right to the line “Hello World!” symbol ★

StringWithStars = Symbol("9733")+"Hello World!"+Symbol("9733"); Report(StringWithStars);

The result of executing this code will be the display of the line “★Hello World!★”

Symbol Code

SymbolCode(<Строка>, <НомерСимвола>) . Gets the Unicode character code from the string specified in the first parameter, located at the position specified in the second parameter.

Example 14. Find out the code of the last character in the line “Hello World!”

String = "Hello World!"; CharacterCode = CharacterCode(String, StrLength(String)); Notify(CharacterCode);

The result of executing this code will be the display of the symbol code “!” - 33.

EmptyString

EmptyString(<Строка>) . Checks whether the string consists of only insignificant characters, that is, whether it is empty.

Example 15. Check whether a string consisting of three spaces is empty.

Empty = EmptyString(" "); Report(Empty);

The result of executing this code will be the display of the word “Yes” (a string expression of a logical value True).

PageReplace

StrReplace(<Строка>, <ПодстрокаПоиска>, <ПодстрокаЗамены>) . Finds all occurrences of the search substring in the source string and replaces it with the replacement substring.

Example 16. In the line “Hello World!” replace the word “Peace” with the word “Friends”.

String = StrReplace("Hello World!", "World", "Friends"); Report(String);

The result of executing this code will be the display of the line “Hello Friends!”

StrNumberLines

StrNumberRow(<Строка>) . Allows you to count the number of lines in a multiline string. To go to a new line in 1s 8, use the symbol PS(newline character).

Example 17. Determine the number of lines in the text:
"First line
Second line
Third line"

Number = StrNumberString("First line"+Characters.PS +"Second line"+Symbols.PS +"Third line"); Report(Number);

The result of executing this code will be the display of the number of lines in the text: 3

StrGetString

StrGetString(<Строка>, <НомерСтроки>) . Gets a line in a multiline string by its number. Line numbering starts from 1.

Example 18. Get the last line in the text:
"First line
Second line
Third line"

Text = "First line" + Symbols. PS + "Second line" + Symbols. PS + "Third line"; LastRow = StrGetRow(Text, StrNumberLines(Text)); Report(LastLine);

The result of executing this code will be the display of the line “Third Line”.

PageNumberOccurrences

StrNumberOccurrences(<Строка>, <ПодстрокаПоиска>) . Returns the number of occurrences of the specified substring in a string. The function is case sensitive.

Example 19. Determine how many times the letter “c” appears in the line “Lines in 1s 8.3 and 8.2”, regardless of its case.

Line = "Lines in 1s 8.3 and 8.2"; NumberOccurrences = StrNumberOccurrences(Vreg(String), "C"); Report(NumberOccurrences);

The result of executing this code will be the display of the number of occurrences: 2.

PageStartsWith

StrStartsWith(<Строка>, <СтрокаПоиска>) . Checks whether the string passed in the first parameter begins with the string in the second parameter.

Example 20. Determine whether the TIN of the selected counterparty begins with the number 1. Let in the variable Counterparty Counterparties.

TIN = Counterparty.TIN; StartsUNits = StrStartsWith(TIN, "1"); If Starts with units Then //Your code EndIf;

PageEndsOn

StrEndsWith(<Строка>, <СтрокаПоиска>) . Checks whether the string passed in the first parameter ends with the string in the second parameter.

Example 21. Determine whether the TIN of the selected counterparty ends with the number 2. Let in the variable Counterparty a reference to a directory element is stored Counterparties.

TIN = Counterparty.TIN; EndsWithTwo = StrEndsWith(TIN, "2"); If EndsInTwo Then //Your code EndIf;

PageSplit

StrDivide(<Строка>, <Разделитель>, <ВключатьПустые>) . Splits a string into parts using the specified delimiter characters and writes the resulting strings to an array. The first parameter stores the source string, the second contains the string containing the separator, the third indicates whether empty strings should be written to the array (by default True).

Example 22. Let us have a string containing numbers separated by the symbol “;”, get an array of numbers from the string.

String = "1; 2; 3"; Array = StrDivide(String, ";"); For Count = 0 By Array.Quantity() - 1 Cycle Attempt Array[Count] = Number(AbbrLP(Array[Count])); Exception Array[Sch] = 0; EndAttemptsEndCycle;

As a result of execution, an array with numbers from 1 to 3 will be obtained.

PageConnect

StrConnect(<Строки>, <Разделитель>) . Converts the array of strings from the first parameter to a string containing all the elements of the array separated by the delimiter specified in the second parameter.

Example 23. Using the array of numbers from the previous example, get the original string.

For Account = 0 By Array.Quantity() - 1 Cycle Array[Act] = String(Array[Act]); EndCycle; Row = StrConnect(Array, "; ");

The String type is found in all programming languages. It is primitive, and in 1C there are many functions for working with it. In this article we will look in detail at various ways of working with string types in 1C 8.3 and 8.2 using examples.

Line

In order to convert a variable of any type into a string, there is a function of the same name “String()”. The input parameter will be the variable itself, the string representation of which must be obtained.

String(False) // returns "No"
String(12345) // returns "12,345"
String(CurrentDate()) //"07/21/2017 11:55:36″

It is possible to convert not only primitive types to a string, but also others, for example, elements of directories and documents.

SokrLP, SokrL, SokrP

The input parameters of these functions are a variable of string type. The functions remove insignificant characters (spaces, carriage returns, etc.): from the left and right sides, only from the left side, and only from the right, respectively.

Abbreviation(" Spaces will be removed on both sides ") // "Spaces will be removed on both sides"
Abbreviation(" Spaces on both sides will be removed ") // " Spaces on the left will be removed "
Abbreviation(" Spaces on both sides will be removed ") // " Spaces on the right will be removed"

Leo, Right, Medium

These functions allow you to trim part of a string. The "Left()" function will return the portion of a string from its left side of the specified length. The "Right()" function is similar, but crops from the right. The “Avg()” function allows you to specify the number of the character from which the line will be selected and its length.

Lev("String variable", 4) // returns "Str"
Right("String variable", 7) // returns "variable"
Medium("String variable", 2, 5) // returns "troco"

StrLength

The function determines the number of characters that are contained in a string variable.

StrLength("Word") // the result of execution will be the number 5

Find

The function makes it possible to search for part of a string in a string variable. The return value will be a number that shows the position of the beginning of the found string. If no match is found, zero is returned.

Please note that the search is case sensitive. If there is more than one occurrence of the search substring in the original string, the function will return the beginning of the first occurrence.

Find("one, two, one, two, three", "two") // function will return the number 6

EmptyString

Using this function allows you to determine whether a string is empty. Minor characters such as space, carriage return, and others are not taken into account.

EmptyString("Pupkin Vasily Ivanovich") // function will return the value False
EmptyString(" ") // function will return the value True

VReg, NReg, TReg

These functions are very useful when comparing and converting string variables. "Vreg()" will return the original string in uppercase, "HPreg()" in lowercase, and "TReg()" will format it so that the first character of each individual word is capitalized, and all subsequent characters are capitalized.

VReg("GENERAL DIRECTOR") // return value - "GENERAL DIRECTOR"
NReg(“CEO DIRECTOR”) // return value – “CEO”
TREG(“CEO DIRECTOR”) // return value – “General Director”

PageReplace

This function is analogous to replacement in text editors. It allows you to replace one character or set of characters with another in string variables.

StrReplace("red, white, yellow", ","", ";") // returns "red; white; yellow"

StrNumberLines

The function allows you to determine the number of lines separated by carriage returns in a text variable.

The loop in the example below will go through three rounds because the LineNumberRow function will return the value 3:

For ind = 1 by StrNumber of Strings ("Line1" + Symbols.PS + "Line2" + Symbols.PS + "Line3") Cycle
<тело цикла>
EndCycle;

StrGetString

This function works with multiline text in the same way as the previous one. It allows you to get a specific string from a text variable.

StrGetString("String1" + Characters.PS + "String2" + Characters.PS + "String3", 2) // return "String2"

PageNumberOccurrences

The function counts the number of occurrences of a character or substring in the search string.

StrNumberAttachments("a;b;c;g; ", ";") // function will return the number 4

Symbol and Symbol Code

These functions allow you to obtain a character by its code in the Unicode encoding, as well as determine this code by the character itself.

SymbolCode("A") // function will return the number 1,040
CharacterCode(1040) // function will return “A”

Common tasks when working with strings

Concatenating Strings

To combine several strings (to perform concatenation), it is enough to use the addition operator.

“Line 1″ + “Line 2″ //the result of adding two lines will be “Line 1 Line 2”

Type Conversion

In order to convert a type into a string, for example, a link to a directory element, a number, etc., it is enough to use the “String()” function. Functions like “ScrLP()” will also convert variables into a string, but immediately with cutting off insignificant characters.

String(1000) // returns "1,000"

Please note that when converting a number to a string, the program automatically added a space separating the thousand. In order to avoid this, you can use the following constructions:

StrReplace(String(1000),Characters.NPP,"") // returns "1000"

String(Format(1000,"HG=")) // returns "1000"

Quotes in a string

Quite often you will have to deal with the need to specify quotation marks in a string variable. This can be either the request text written in the configurator, or just a variable. To solve this problem, you just need to set two quotation marks.

Header = String("Horns and Hooves LLC - that's us!") // will return "Horns and Hooves LLC - that's us!"

Multi-line, line break

In order to create multiline text, just add line breaks (Symbols.PS) to it.

MultilineText = “First Line” + Symbols.PS + “Second Line”

How to remove spaces

In order to remove spaces on the right or left, you can use the function “ScrAP()” (as well as “ScrL()” and “ScrP()”):

StringNoSpaces = Abbreviation(" Many letters ") // the function will return the value "Many letters"

If, after converting a number to a string, you need to remove non-breaking spaces, use the following construction:

StringNoSpaces = StrReplace(String(99999),Characters.NPP,"") // returns "99999"

Programmers also often use the following construction, which allows you to remove or replace with another character all spaces in a text variable:

StringNoSpaces = StrReplace("hello","") // returns "hello"

Comparing strings with each other

You can compare terms with the usual equal sign. The comparison is case sensitive.

"Hello" = "hello" // returns False
"Hello" = "Hello" // returns True
"Hello" = "Goodbye" // will return False


Close