This article translates the JavaScript Tutorial playlist of up master Kudvenkat on YouTube
SOURCE Address here:
https://www.youtube.com/watch?v=PMsVM7rjupU&list=PL6n9fhu94yhUA99nOsJkKXBqokT3MBK0b
The following method is a method that can be used in JavaScript to get substring in a string:
SUBSTRING ()
SUBSTR ()
Slice ()
SUBSTRING () Method: This method has two parameters, starting and ending. The starting parameter is required, and it determines the starting position of the extraction. The end parameter is not required, and it determines the end position of the fetch. The word at the end of the line is not included in the substring. If the end parameter defaults, The part that starts from the beginning to the end of the entire string is extracted. If the starting parameter value is greater than the end parameter value, this method automatically swaps two parameters. That is, the starting position becomes the end position, and the end position becomes the starting position.
Example: Extraction of the first 10 letters
var str = "JavaScript Tutorial"; var result = str.substring (0, ten); alert (result);
Output:javascript
If the value of the starting parameter is greater than the end parameter, then the two parameters are interchanged
var str = "JavaScript Tutorial"; var result = str.substring (ten, 0); alert (result);
Output:javascript
Substr () Method: This method has two parameters, starting and counting. The starting parameter is required, which determines the starting position of the extraction substring. The count parameter is not required, and it determines how many letters to extract from the starting position. If the count parameter defaults, All letters at the end of the string are extracted from the starting position. If the count parameter is 0 or a negative number, an empty string is returned.
Extract the first 10 letters
var str = "JavaScript Tutorial"; var result = Str.substr (0, ten); alert (result);
Output:javascript
If the count parameter is the default, all letters of the remaining string are extracted from the starting position
var str = "JavaScript Tutorial"; var result = Str.substr (one); alert (result);
Output:tutorial
Slice () Method: This method has two parameters, start and end, the starting parameter is required, it determines the starting position of the extraction. The end parameter is not required, and it determines the location of the end point. The letter of the end position is not included in the extracted substring. If the endpoint parameter defaults, All the remaining string letters from the starting position will be extracted.
Extract the first 10 letters
var str = "JavaScript Tutorial"; var result = Str.slice (0, ten); alert (result);
Output:javascript
If the endpoint parameter is default, all letters starting from the start position to the end of the string will be extracted
var str = "JavaScript Tutorial"; var result = Str.slice (one); alert (result);
Output:tutorial
Differences between the substr () and substring () methods
The difference is in the second argument, where the second parameter of substring indicates where the index of the end of the fetch ends. The letter at the end position is not included by substring. The second argument of substr () indicates how many letters to return
Another difference is that the substr () method cannot be run on IE8 and earlier versions of the
The difference between slice () and substring ()
If the starting parameter is greater than the end parameter, then substring () swaps the position of two values, but slice () does not
Another useful method in extracting substring is the indexof () method. This method returns the first encountered position of a specific value in a string. Returns 1 if a specific value is not found
Example: Find the location of the @ letter in email
var str = "[email protected]"; var result = Str.indexof ("@"); alert (result);
Output:6
Translation The substring in Javasctipt