Q: Which of the following String object functions returns the character in the string beginning at the provided position through the specified number of characters?
-
A
slice()
-
B
split()
-
C
substr()
-
D
search()
C
Answer:
C
Explanation:
The substr() method extracts a specified number of characters from a string,
starting at a specified index position.
Here's an example of how to use it:
let str = 'Hello World';
let sub = str.substr(6, 5); // Extract 5 characters starting at index 6
console.log(sub); // Output: "World"
The substr() method is similar to the substring() method, which also extracts characters from a string.
The main difference between the two is that substring() does not allow negative indexes, whereas substr() does.
Related Topic:
Share Above MCQ