JavaScript Cut Method
The cut()
method is a built-in JavaScript function that can be used to extract a portion of a string and return a new string. The extracted portion can be specified by providing the starting index and the length of characters to be extracted.
Syntax
The syntax of the cut()
method is as follows:
string.cut(startIndex, length)
string
: The original string from which a portion is to be extracted.startIndex
: The index position from which the extraction is to begin.length
: The number of characters to be extracted.
Example
const str = "JavaScript is awesome";
const newStr = str.cut(0, 10);
console.log(newStr); // Output: "JavaScript"
Output
The cut()
method returns the newly extracted string.
Explanation
In the above example, the cut()
method is used to extract a portion of the string. The startIndex
parameter is set to 0
, which means that the extraction will begin from the first character of the string. The length
parameter is set to 10
, which means that a total of 10 characters will be extracted starting from the first character of the string. The extracted portion is then returned as a new string.
Use
The cut()
method can be used to extract a portion of a string based on a specific index position and length of characters.
Important Points
- The
cut()
method can only be used with string data types. - The
startIndex
andlength
parameters must be integers. - The
cut()
method does not modify the original string, instead, it returns a new string.
Summary
The cut()
method is a powerful string function in JavaScript that can be used to extract a portion of a string based on a specific index position and length of characters. It is a useful tool for manipulating and working with strings in JavaScript.