The JavaScript browser language provides the developer with the ability to efficiently work with strings and full access to the DOM tree, which builds for every page opened by a site visitor.
The arsenal of string functions available through the methods of the string object is quite large. Converting various data types to strings is done automatically when the need arises. Retrieving data through the JavaScript substring () method is a sought-after operation.
Syntax and Method Usage
Any line operation manipulates characters. First of all, it is mandatory accounting coding. The developer should keep in mind: it is important not only in what encoding the string is . It is very important in which encoding the page is located , in which the code processing the string is located.
The JavaScript substring () method has two parameters: the number of the character from which the extraction begins and the number of the character before which the extracted substring ends.
Substring Examples
In the given examples, two lines of the keyboard in Latin and Cyrillic coding are used. The action for various parameters of the method is shown.
It is allowed to experiment with zero and negative values, but the effect will be adequate. The above JavaScript substring example convinces us that it is preferable to use reasonable values for the start of the extraction and its end, rather than experimenting.
The method of extracting a substring from a string refers to the first character of the substring and the one that follows the last. Importantly, the second parameter is not the quantity, but the symbol number. This is not a familiar decision. Usually, in operations of extracting a substring from a string, “with what and how much” is indicated. In the JavaScript method, substring () is not the case: both parameters are position numbers, both can be zero, “NaN” or have a negative value.
Theory and practice of line operations
JavaScript is a very beautiful and popular programming language. In one way or another, every qualified developer must know it, even if his area of competence is only server scripts or cascading style sheets.
Everything is interconnected. You can not take an active part in the development of the site without having any idea about the language of the browser. Usually JavaScript courses offer another method of a string object - substr:
- 'string'.substr (Position of the First Character [, Length of the Substring Extracted])
Here, the retrieved string is characterized by the start position and length. A familiar decision that is made in other languages. But JavaScript is a special language.
JS cannot be positioned separately from other programming languages, but its close connection with the browser, the DOM page tree, and the features of the “environment” leads to features of the implementation of functionality and features in its use. In particular, the above concerns the JavaScript substring () method.
The recoverable substring is far from known in all cases, both in its position and in its length. By and large, there is no work with strings in JS. Here they work with objects, in particular, with lowercase objects. The JavaScript substring () method is the functionality of a string object, just like substr (). The first one deals with extracting a substring:
- position of the beginning and end;
second:
- start position and length.
This is convenient for compact recording of the algorithm in each case for specific reasons. In the following example (four functions that remove extra spaces to the left and right of significant characters in a string), using substr is convenient.
JavaScript allows you to formulate the algorithm as concisely as possible without unnecessary "words". In some cases, substr is convenient, in others pop and push - although they are not related to strings at all (these are array methods), but when using the split and join pair, you can look at the use of substring in a completely different way.
JavaScript opens up possibilities. How and what to use, the developer determines.