cut a string in Javascript
Code snippet on how to cut a string in Javascript
let str = "The quick brown fox";
let cutStr = str.substring(4, 10);
This code takes a string, “The quick brown fox”, and cuts it to a new string “quick”. The new string is stored in the variable cutStr
. The code achieves this by using the substring()
method. This method takes two numbers as arguments - the starting index and the ending index - and returns the string in the range of the two indexes. The starting index is inclusive and the ending index is exclusive, so cutStr
contains all the characters from index 4 up to, but not including 10. In this case, it takes the characters “quick” from “The quick brown fox”.