Converting a string to a number 1s 8.3. New functions for working with strings. String functions

PART 1. METHODS (26).

1.1. Basic methods (10). Independent, built on their own simple algorithms.

Str_Complete () (right-left padding with specified characters).

Str_Inverse () (inversion - characters from right to left).

Str_Codes () (character codes through "," and an array with codes)

Str_Symbols () (a string from codes through ”,” or from an array with codes)

Str_ReplaceCharacters () (replace some characters with others).

String_FromArray () (string from an array of fragments separated by a separator)

Str_FindIn () (search for a substring (including on the right) with the specified entry number).

Str_FindGr () (Search for a group of characters in the specified character set)

Page_FindNumber () (Search for a number, including on the right with the specified entry number)

Str_Interpret () (into an array, according to the established syntax)

1.2. Derivative methods (12). Actually, this is the use of four generalized methods-algorithms (see Part 2)

Str_Number () (from the beginning and from the end of the line, do not "be afraid" of non-numeric characters.

Str_Find () (search for a substring (including case insensitive and to the right) with the specified entry number).

Str_Replace () (search (including case insensitive and to the right), and replace separator substrings).

Str_Part () (a piece of string between the specified occurrences of the delimiter (left or right)).

Str_ReplacePiece () (replacing "piece" in the original string with the specified string).

Str_BArray () (between the specified occurrences of the separator (including on the right and without case).

Str_WrapBySyllables () (split into substrings “Hard”, with a hyphen).

Str_WrapByWords () (split into substrings "Softly")

Str_Cut () (“Cut” into substrings of specified lengths)

Str_SokrL () (replace the left group of "abbreviated" characters with "replacement string"

Str_Screw () (replace the right group of "abbreviated" characters with "replacement string"

Str_CarS () (replace in the middle of the group of "abbreviated" characters with "replacement string"

Str_Expand (expand to specified length by increasing the number of specified characters)

1.3. Detailed methods (3). "Dissection" of a row with transfer to a table with detailed information.

Str_inTableIn () (into the table according to the nested delimiter system).

Page_inTableGr (into a table according to a multilevel filter).

Page_inTableNumbers (in a table with numbers and fragments between them).

PART 2. GENERALIZED ALGORITHM METHODS (3).

Str_Entry () ("Find" and "Replace" methods).

Str_Fragments () (methods "Piece", "ReplacePiece," BArray "," inTableVx ").

Str_Socr () (methods "Abbreviated", "Abbreviated", "Abbreviated", "Expand".

Str_Split () (methods "MoveBySyllables", "MoveByWords", "Cut").

PART 3. UNIVERSAL FUNCTION.

It is a kind of conditional programming interface that allows

apply multiple methods to a string at once. Implemented as

functions with seven parameters ("Demo" is built on this function):

Page_ (Methods, Submethods, Input, Param1, Param2, Length_Position_Number, AdditionalOutput)

Parameters:

- "Methods" - several "combined" and (or) one "exclusive" method

(single-character codes or names, you can use ",")

- "Submethods" - several "combined" and (or) "exclusive" options

"Exclusive" method (one-character codes or names);

- “Input” - String, or Array or Table of values;

- "Param1" - search string, replacements, separators, filters;

- “Param2” - replacement string or symbols;

- "Length_Number_Position" -Number, Numbers through a separator or an array with Numbers;

- "Additional Exit" - Number or String or Array or Table of values;

Names and (or) single-character codes of methods-sub-methods, as well as numbers in

(Length_Number_Position) can be in any register and separated

any of the following delimiters: ”,:;”.

PART 4. SOME EXAMPLES.

YesNumberInString \u003d (String_FindNumber (InString)<>Undefined);

HaveDigitsVSString \u003d (Str_FindGr (InString, "+ 0123456789")\u003e 0);

YesLatin \u003d (Str_FindGr (VxStr, Str_Interpret ("lL"))\u003e 0);

There areSpecifiedSigns \u003d (Str_NnaytGr (VxStr, "+ to rYu.0p9")\u003e 0);

YesNotPrinted \u003d (Str_FindGr (VxStr, Str_Interpret ("- l-L-r-R-P-Z-Ts"))\u003e 0);

PART 5. CONCLUSION.

Where possible, I got by with one pass along the line. The second pass is usually already in fragments. Didn't use the built-in function StrNumber of Occurrences ().

Built-in functions used: Lion (), Right () Average (), StrLength ()

- (positioning and getting part of the string - must be "fast").

Implemented in version 8.3.6.1977.

We have expanded the set of functions for working with strings. We did this in order to give you more advanced tools for parsing string data. New functions will be convenient and useful in technological tasks of text analysis. In tasks related to parsing text that contains formatted data. This can be the analysis of some files received from the equipment, or, for example, the analysis of a technological log.

All actions that perform new functions, you could have performed before. With the help of more or less complex algorithms written in an embedded language. Therefore, the new functions do not give you any fundamentally new possibilities. However, they can reduce the amount of code, make the code simpler and easier to understand. And besides that, they allow you to speed up the execution of actions. Because the functions implemented in the platform work, of course, faster than a similar algorithm written in an embedded language.

Formatting function StrPattern ()

This function substitutes parameters into a string. The need for such a conversion often arises, for example, when displaying warning messages. The syntax for this function is as follows:

PageTemplate (<Шаблон>, <Значение1-Значение10>)

<Шаблон> is a string to substitute for the parameter representations.

<Значение1> , ... <Значение10> - these are parameters (maximum ten), the representations of which must be substituted into the string.

To indicate a specific place in the template where you want to perform substitution, you need to use markers like% 1, ...% 10. The number of markers used in the template and the number of parameters containing values \u200b\u200bmust match.

For example, the result of executing such a statement:

there will be a line:

Data error on line 2 (Date type required)

Function for working with strings StrCompare ()

This function compares two strings in a case insensitive manner. For example, like this:

You could have performed the same action earlier using the Value Comparison object:

However, using the new function looks simpler. Besides, the function, unlike the Comparison of Values \u200b\u200bobject, works both in the thin client and in the web client.

Functions for working with strings RowStart With (), RREnd With ()

These functions determine whether a string begins with a specified substring or ends with a specified substring. The algorithm of these functions is easy to implement in an embedded language, but their presence allows you to write cleaner and more understandable code. And they work faster.

For example, it is convenient to use them in the If:

Functions for working with strings StrSplit (), StrConnect ()

These functions split the string into parts according to the specified delimiter. Or vice versa, they join several lines into one, inserting the selected separator between them. They are convenient for creating or analyzing logs, technological log. For example, you can easily disassemble a record of a technological log into parts suitable for further analysis:

Function for working with strings StrFind ()

Instead of the old Find () function, we have implemented a new function that has additional capabilities:

  • Search in different directions (from the beginning, from the end);
  • Search from a specified position;
  • Search for an occurrence with the specified number (second, third, etc.).

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

Below is an example using the new search capabilities. Backward searches are useful when you need the last piece of a formalized string, such as the fully qualified filename in a URL. And searching from a specified position helps in cases when you need to search in a known fragment, and not in the entire string.

Hello
Today they asked me not to display the prefix of the document when printing the implementation document, the problem was solved as follows.

PageReplace (Number, Leo (Number, 3), "");

When I looked into the syntax assistant, I liked it so much that I wanted to write about working with strings in 1C: Enterprise.

A string in 1C refers to a primitive data type.

If you look at the 1C help, we will see the following description of the String type
Values \u200b\u200bof this type contain an arbitrary length Unicode string.
If in your own words about strings it will work. A string is a constant consisting of various characters, always surrounded by quotes.

Setting a string
Call \u003d "Hello Passenger";

Multi-line strings in typical configurations are most often created using the vertical "|" separator:
String \u003d "Multi-line
| string
| write
| text ";

The quotation mark inside the string is specified by double quotation marks.
String \u003d "" Text in quotation marks "";

There are many operations on strings (search in the inside of a string, determining the first, etc.), it is better to look at the Syntax Helper
Syntax Assistant - General description of the built-in language-\u003e built-in functions-\u003e functions for working with strings.

String functions

Here in a short form I will give functions for working with strings:

BReg (<Строка>) - The function converts all characters in the string to uppercase.

SymbolCode (<Строка>, <НомерСимвола>) - The function receives the code of the character located in the passed string at the position with the specified number.

A lion(<Строка>, <ЧислоСимволов>) - The function selects the first characters from the left of the string.

To find(<Строка>, <ПодстрокаПоиска>) - The function finds the occurrence of the search string as a substring in the original string.

HPreg (<Строка>) - The function converts all characters in the string to lowercase.

Right (<Строка>, <ЧислоСимволов>) - This function differs from the Left functions in that it selects the last characters of the string on the right.

Empty line(<Строка>) - The function checks the string for significant characters.

Symbol(<КодСимвола>) - The function converts the character code to a string containing the character.

AbbrL (<Строка>) - The function cuts off insignificant characters to the left of the first significant character in the string.

SocrLP (<Строка>) - The function strips off insignificant characters to the left of the first significant character in the string and spaces to the right of the last significant character in the string.

Abbreviation (<Строка>) - The function cuts off insignificant characters to the right of the last significant character in the string.

Wednesday (<Строка>, <НачальныйНомер>, <ЧислоСимволов>) - The function selects a character string starting with a character<НачальныйНомер>, in total<ЧислоСимволов>.

StrLength (<Строка>) - The function gets the number of characters in a string.

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

StrGetString (<Строка>, <НомерСтроки>) - The function gets a string of a multi-line string by number.

StrNumber of Occurrences (<Строка>, <ПодстрокаПоиска>) - The function calculates the number of occurrences of the search substring in the original string.

StrNumber of Lines (<Строка>) - The function counts the number of lines in a multi-line string. In a multi-line string, the lines are separated by line feed characters.

Treg (<Строка>) - The function converts the string to the title case. This means that for each word, the first character is converted to a title case or to upper case if no title case is defined for the character. The rest of the characters are converted to lowercase.

Type conversions
For explicit conversion of data types, there are functions of the same name with the data type to which the conversion occurs: String (<Значение>)

StringOfNumbers \u003d String (Number);

All these functions are described in detail in the Syntax Assistant, how the Syntax Assistant can help a beginner programmer, I described in the article.

Examples for working with strings

Convert a number to a string and back.

To get a string representation of the year, use the Format function.

Year \u003d Format (CurrentDate (), "DF \u003d yyyy") // Year \u003d "2012"

To convert a number to a string without inserting a group separator character (non-breaking space), you must use the Format function with the ChG \u003d 0 parameter:

Number \u003d 2012 String \u003d Format (Number, "CHG \u003d 0"); // String \u003d "2012"

String No spaces.

Notes from the Looking Glass

08/18/2014 New functions for working with strings

Implemented in version 8.3.6.1977.

We have expanded the set of functions for working with strings. We did this in order to give you more advanced tools for parsing string data. New functions will be convenient and useful in technological tasks of text analysis. In tasks related to parsing text that contains formatted data. This can be the analysis of some files received from the equipment, or, for example, the analysis of a technological log.

All actions that perform new functions, you could have performed before. With the help of more or less complex algorithms written in an embedded language. Therefore, the new functions do not give you any fundamentally new possibilities. However, they can reduce the amount of code, make the code simpler and easier to understand. And besides that, they allow you to speed up the execution of actions. Because the functions implemented in the platform work, of course, faster than a similar algorithm written in an embedded language.

Formatting function StrPattern ()

This function substitutes parameters into a string. The need for such a conversion often arises, for example, when displaying warning messages. The syntax for this function is as follows:

PageTemplate (<Шаблон>, <Значение1-Значение10>)

<Шаблон> is a string to substitute for the parameter representations.

<Значение1> , ... <Значение10> - these are parameters (maximum ten), the representations of which must be substituted into the string.

To indicate a specific place in the template where you want to perform substitution, you need to use markers like% 1, ...% 10. The number of markers used in the template and the number of parameters containing values \u200b\u200bmust match.

For example, the result of executing such a statement:

there will be a line:

Data error on line 2 (Date type required)

Function for working with strings StrCompare ()

This function compares two strings in a case insensitive manner. For example, like this:

You could have performed the same action before using the object Comparison of Values:

However, using the new function looks simpler. And besides this, the function, unlike the object Comparison of Values, works in both the thin client and the web client.

Functions for working with strings RowStart With (), RREnd With ()

These functions determine whether a string begins with a specified substring or ends with a specified substring. The algorithm of these functions is easy to implement in an embedded language, but their presence allows you to write cleaner and more understandable code. And they work faster.

For example, it is convenient to use them in the operator If:

Functions for working with strings StrSplit (), StrConnect ()

These functions split the string into parts according to the specified delimiter. Or vice versa, they join several lines into one, inserting the selected separator between them. They are convenient for creating or analyzing logs, technological log. For example, you can easily disassemble a record of a technological log into parts suitable for further analysis:

Function for working with strings StrFind ()

Instead of the old function To find() we have implemented a new feature that has additional features:

  • Search in different directions (from the beginning, from the end);
  • Search from a specified position;
  • Search for an occurrence with the specified number (second, third, etc.).

In fact, it duplicates the capabilities of the old function. This is done in order to maintain compatibility with modules compiled in older versions. Old function To find() not recommended to use again.

Below is an example using the new search capabilities. Backward searches are useful when you need the last piece of a formalized string, such as the fully qualified filename in a URL. And searching from a specified position helps in cases when you need to search in a known fragment, and not in the entire string.

The basic capabilities of a programming language usually include working with numbers and strings. Usually these features are hardcoded into the compiler code (or the "base" classes of the programming language are implemented).

In 1C, the ability to work with strings is programmed in the platform itself. Today we will consider the features of working with 1C strings in programs in the built-in 1C language.

Line 1C value

1. Let's start with the simplest. Creating a variable and assigning a constant string value to it looks like this in 1C:

Variable \u003d "Hello world!";

If you need to specify a quote character in a constant string value 1C, then you need to double it ""

Variable \u003d "Hello," "world" "!";

2. Line break 1C can be specified in two ways at once. The first one is using the symbol |

Variable \u003d "Hello,
| peace! ";

The second is using the Symbols system enumeration. It allows you to add both 1C line breaks and other non-printable characters, such as TAB.

Variable \u003d "Hello" + Symbols.PS + "world!";

3. Configurations in 1C can be developed not only for one language (Russian, English or another) - but simultaneously for several languages. In this case, the currently used language is selected at the bottom of the 1C window.

The list of languages \u200b\u200bis in the configuration window in the General / Languages \u200b\u200bbranch. Each language has a short identifier such as ru or eng.

It is clear that when programming such a configuration, 1C strings can also be multilingual. To do this, it is possible to create such a 1C line by specifying through; options by language identifier:

Variable \u003d "ru \u003d" "Hello, world!" "; En \u003d" "Hello, world!" "";

If you use the 1C line formed in this way as usual, then it will be what is written in it. In order for the system to split it into two options and use the desired one, you must use the НСтр () function:

// correct for bilingual configurations
Report (НStr (Variable));

Props with line type 1C

A props is a field of a 1C reference / document. It differs from a variable in a 1C program in that its type (number, 1C string, etc.) is precisely indicated for the props. If you need to brush up on what props are - watch the lesson on.

If you specify the type of attribute - line 1C, then you must additionally specify the parameters.

1C strings are of unlimited length (specified as length \u003d 0) and limited length, indicating the exact number of characters. 1C rows of unlimited length are stored in a separate SQL table, so their use is less productive than limited.

That is why the use of 1C strings of unlimited length has its limitations - it is not possible to use them everywhere. For example, it cannot be used as a document number, directory code, dimension.

Working with 1C strings

There are several built-in functions of the 1C platform for working with strings.

  • SocrLP ("Unbelievable, but true!")
    Removes extra spaces from line 1C. It can also be used to convert any types to 1C string (for example, numbers).
  • Variable \u003d "Vasya" + SokrLP ("plus") + "Olya"; // will be "Vasya plus Olya"
    An example of summing several 1C string values. The result will be one line of 1C.
  • Variable \u003d Leo ("Music", 2); // will be "Mu"
    Variable \u003d Wednesday ("Music", 2, 2); // will be "shl"
    Variable \u003d Right ("Music", 2); // will be "ka"
    Various options for obtaining a substring from a 1C string.
  • Variable \u003d Find ("Music", "Shl"); // will be 3
    Search for a substring in string 1C, starting from character 1.
  • Variable \u003d StrLength ("Music"); // will be 6
    Returns the number of characters in line 1C.
  • Report ("Hello") // in the message window at the bottom of the 1C window
    Warning ("Hello") // popup dialog
    State ("Hello") // in the status line at the bottom left
    .

Casting objects to line 1C

As you know, the most popular format for exchanging structured information at the present time is XML. Even the latest version of MS Office Word and Excel save files in this format (docx and xlsx, respectively, change the extension to zip, open in the archiver).

The 1C platform for data exchange provides several options, the main of which is also XML.

1. The simplest method is to use the AbscrLP () or String () function. You can use the REPRESENTATION () function in the query body. The result of their action is the same - they generate a string representation of any 1C object for the user.

For a reference book, this will be its name by default. For a document - the name of the document, number and date.

2. Any 1C object (with restrictions) can be converted to XML and vice versa. The conversion process is called serialization.

ViewStringXml \u003d XMLString (Value); // get XML from 1C value
Value1C \u003d XMLValue (Type ("ReferenceLink.Nomenclature"), ViewStringXml); // get the 1C value from the XML string, you must specify the 1C type that should be received

3. There is a way of the 1C platform to convert any 1C object into a string. He migrated from version 1C 7.7. This format is not understood by other programs, but it is understood by another 1C, which makes it easy to use it for exchange between 1C bases.

String \u003d ValueVSStringInter (Value1C); // get line 1C from value 1C
ValueVFile ("C: \\ MyFile.txt", Value1C); // another option, we get a file with a saved string from the 1C value
Value1C \u003d ValueFromStringInternal (String); // back from line 1C
Value1C \u003d ValueOfFile ("C: \\ MyFile.txt"); // back from file

Editing 1C lines on the form

In addition to working with 1C strings in a 1C program, of course, I would like the user to be able to edit them. There are several possibilities for this:

1. The easiest way is to request input of the 1C line on demand. This method is used when teaching 1C programming, in real life it is used much less often (but it is used!).

Variable \u003d "";
String \u003d EnterValue (Variable, "Enter your full name");

2. To display the attribute of a 1C object (reference book / document) or a form attribute (see), an input field is most often used. This is the most common tool in 1C for user work with editing fields.

3. The possibilities of the input field can be expanded (see the properties of the input field, right-click on it, for more details):

  • Checkmark Multi-line editing mode
  • Checkbox Advanced editing (available if the previous checkbox is checked)
  • Checkbox Password mode (see).

4. If you lack all the capabilities of the input field, there is a built-in editor. To add it to the form, add a Text Document Field in the Form / Insert Control menu. In its properties, you can specify the mode of its operation - the Extension property.

A text document field cannot be linked directly to data. It is necessary to write a function in the OnOpen () event handler of the form (see):

Form Elements.ElementNameTextDocumentField.SetText (ValueString); // here ValueString is the text obtained, for example, from the props

And in the save handler - for example, in the Save button - add a save:

ValueString \u003d FormElements.ElementNameTextDocumentField.GetText (); // ValueThe string here is the props where we store the value

5. In 1C version 8.2.11, in managed forms, there is a new opportunity to represent the 1C line - the Formatted document field.


Similar to the field of a text document, you need to set when opening and write when saving the text yourself using the program.

  • In the 1C object, the form of which we are making (reference book, document, processing, etc.) - add an attribute with the Storage type
  • In the ReadOnServer () function, set the text from the props

    // here Props is the added property of the 1C object
    // here FormattedDocument is the name of the field on the form to edit
    &On server

    FormattedDocument \u003d CurrentObject.Props.Get ();
    End of Procedure

  • In the function BeforeWriteOnServer () or by the button, write the text from the field

    &On server
    Procedure for ReadingOnServer (CurrentObject)
    CurrentObject.Props \u003d NewValueStore (FormattedDocument);
    End of Procedure