Javascript: working with strings, functions

Object-oriented features and associative arrays of JavaScript as semantic “frameworks” for using functions and constructs for processing strings are of particular interest for programming information processing processes based on its semantic content. In JavaScript, string functions can be combined into their own semantic constructs, simplifying the code and formalizing the subject area of ​​the task.

javascript string handling

In the classic version, information processing is, first of all, string functions. Each function and language design has its own characteristics in the syntax and semantics of JavaScript. The methods for working with strings here have their own style, but in the usual application it is just a syntax within the framework of simple semantics: search, replace, insert, extract, content, change case ...

Description of String Variables

To declare a string, use the var construct . You can immediately set its value or form it during the execution of the algorithm. You can use single or double quotes for a string. If there must be a quotation mark in it, it must be escaped with the “\” symbol.

The double-quoted string requires escaping of the internal double quotes. Similarly, the one indicated by single is critical to the presence of single quotes inside.

Author's example is not a picture

In this example, the string “str_dbl” lists useful special characters that can be used in the string. In this case, the character "\" is escaped.

A string is always an array

JavaScript can work with strings in many ways. The language syntax provides many options. First of all, one should never forget that (in the context of the descriptions made):

  • str_isV [3] => "V";
  • str_chr [1] => "'";
  • str_dbl [5] => "a".

That is, line characters are available as elements of an array, with each special character being one character. Escaping is a syntax element. No “screen" is placed in the real line.

Using the charAt () function has a similar effect:

  • str_isV.charAt (3) => "V";
  • str_chr.charAt (1) => "'";
  • str_dbl.charAt (5) => "a".

The programmer can use any option.

Basic string functions

In JavaScript, working with strings is slightly different than in other languages. The name of the function is written to the variable name (or the string itself) through a dot. Usually, string functions are called methods in the style of language syntax, but the first word is more familiar.

javascript string methods

The most important string method (more correctly, a property) is its length.

  • var xStr = str_isV.length + '/' + str_chr.length + '/' + str_dbl.length.

Result: 11/12/175 in the lines of the above description.

The most important string pair of functions is splitting a string into an array of elements and merging the array into a string:

  • split (s [, l]);
  • join (s).

In the first case, the string is divided by the separator character “s” into an array of elements in which the number of elements does not exceed the value “l”. If the quantity is not set, then the entire line is broken.

In the second case, the array of elements merges into one line through the specified delimiter.

A notable feature of this pair: splitting can be done by one separator, and merging by another. In this context, in JavaScript, working with strings can be “pushed beyond the limits” of the language syntax.

Classic lowercase functions

Common string processing functions:

  • Search;
  • sample;
  • replacement;
  • conversion.

Represented by methods: indexOf (), lastIndexOf (), substr (), substring (), toLowerCase (), toUpperCase (), concan (), charCodeAt () and others.

Author's example is not a picture

In JavaScript, working with strings is represented by a large number of functions, but they either duplicate each other or are left for old algorithms and compatibility.

For example, using the concat () method is acceptable, but it’s easier to write:

  • str = str1 + str2 + str3;

The use of the charAt () function also makes sense, but the use of charCodeAt () has real practical value. Similarly, for JavaScript, line breaks have a special meaning: in the context of the output to the screen, for example, in the alert () message it is “\ n”, in the construction of the page content generation it is “<br/>”. In the first case, this is just a character, and in the second, a string of characters.

Strings and Regular Expressions

In JavaScript, working with strings includes a regex engine. This allows you to perform complex searches, selections and string conversions inside the browser without accessing the server.

Author's example is not a picture

The match method finds, and replace replaces the found match with the desired value. Regular expressions are implemented in JavaScript at a high level, in essence, they are complex, and due to the specifics of the application they transfer the center of gravity from the server to the client’s browser.

javascript string functions

When applying the match, search, and replace methods , one should not only pay due attention to testing the entire range of acceptable values ​​of the initial parameters and the search strings, but also evaluate the load on the browser.

Regular Expression Examples

The scope of regular expressions for string processing is extensive, but requires great accuracy and attention from the developer. Regulars are primarily used when testing user input in form fields.

Author's example is not a picture

Here are the functions that check whether the input contains an integer (schInt) or a real number (schReal). The following example shows how efficiently it can process strings by checking for only valid characters: schText is text only, schMail is the correct email address.

Author's example is not a picture

It is very important to keep in mind that in JavaScript characters and strings require increased attention to the locale, especially when you need to work with the Cyrillic alphabet. In many cases, it is advisable to indicate real character codes rather than their meanings. Russian letters this applies in the first place.

javascript string character

It should be especially noted that it is far from always necessary to carry out the task as it is set. In particular, with regard to checking integers and real numbers: you can get by not with classical string methods, but with usual syntax constructs.

Object oriented strings

In JavaScript, string manipulation is represented by a wide range of functions. But this is not a good reason for using them in their original form. The syntax and quality of the functions is impeccable, but it is a universal solution.

Any use of string functions involves processing the real meaning, which is determined by the data, the scope, the specific purpose of the algorithm.

javascript with strings

The ideal solution is always to interpret the data according to their meaning.

Representing each parameter as an object, one can formulate functions for working with it. It's always about character processing: numbers or strings are specifically organized sequences of characters.

There are general algorithms, but there are private ones. For example, the surname or house number are strings, but if in the first case only Russian letters are allowed, then in the second case numbers and Russian letters are allowed and hyphens or indices can occur through a forward slash. Indexes can be alphanumeric. A house may have enclosures.

All situations cannot always be foreseen. This is an important point in programming. A rare algorithm does not require refinement, and in most cases it is necessary to systematically adjust the functionality.

javascript line break

Formalization of processed line information in the form of an object improves the readability of the code, allows you to bring it to the level of semantic processing. This is another degree of functionality and significantly better code quality with greater reliability of the developed algorithm.

Source: https://habr.com/ru/post/K22553/


All Articles