4 minute(s) read
Published on: Jun 04, 2021
Updated on: Dec 14, 2021
What is JS?
Strings in this language store text and manipulate it, including several characters placed inside quotes.
Consider the following example.
You can also use single or double quotes:
One of the characteristics that strings can have is their length of it. To understand this case better, consider the following example.
As you know, in this language, strings need to be written within quotes, so when you use each item in quotes, it is assumed that they are all text strings, so if you don't want it to happen, it is to get help from Escape Character, now consider the following examples.
The sequence \ "inserts a double quote in a string:
The sequence \ 'inserts a single quote in a string:
The sequence \\ inserts a backslash in a string:
Can be Objects:
Strings can also be used as objects, for which they must be entered with a new keyword as follows.
var firstName = new String ("John");
Consider the following example in this regard.
Avoid writing code lines while coding:
The goal of all programmers is to make sure that the code line they write does not exceed 80 characters. You can break these lines in different ways. One way to break these lines is within a text string, which is given below.
It should be noted that you cannot do this with a backslash.
There are different methods in this regard, each of which is used to achieve a different goal, some of which we will discuss below.
- indexOf () method:
This method is used to show the first occurrence of a specified text in a text string.
- lastIndexOf () method:
It is the other method that exists used to show the last occurrence of a specified text in a string.
- search () method:
They also use this method to search a string for a specified value and returns the position of the match:
- slice () Method:
This method is also used to extract part of the string and then return the extracted part in a new string.
- substring () Method:
This method is the same as the previous one, and it is used to extract part of the string and then return the extracted part in a new string, but the difference between them is that this method cannot accept negative indexes.
- substr () Method:
This method is similar to the slice () method, and it is used to extract part of the string and then return the extracted part in a new string, but the difference between them is that the second parameter specifies the length of the extracted part.
- replace () method:
As its name implies, this method is used when you want to replace a certain value in a string with another value.
- concat () Method:
To connect two or more strings, you must use this method.
- trim () method:
There may be a space on either side of the string you entered that you want to delete, so you must use this method.
- split () method:
This is used to convert a string to an array.
In general, strings, different methods can be used to change them, some of which we have mentioned in this article, the important point that has been mentioned in this regard was that in coding, you have to avoid writing a long code line because it complicates work and causes other problems.Click to analyze your wesbite SEO