Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Removing first k characters from string in JavaScript
We are required to write a JavaScript function that takes in a string and a number, say k and returns another string with first k characters removed from the string.
For example: If the original string is ?
const str = "this is a string"
and,
n = 4
then the output should be ?
const output = " is a string"
Method 1: Using substr() Method
The substr() method extracts characters from a string starting at a specified position.
const str = 'this is a string';
const removeN = (str, num) => {
const { length } = str;
if(num > length){
return str;
};
const newStr = str.substr(num, length - num);
return newStr;
};
console.log(removeN(str, 3));
console.log(removeN(str, 4));
console.log(removeN(str, 10));
s is a string is a string tring
Method 2: Using slice() Method (Recommended)
The slice() method is the modern and preferred approach for extracting parts of a string.
const removeFirstK = (str, k) => {
if (k >= str.length) {
return "";
}
return str.slice(k);
};
const originalString = "JavaScript Programming";
console.log("Original:", originalString);
console.log("Remove first 4:", removeFirstK(originalString, 4));
console.log("Remove first 10:", removeFirstK(originalString, 10));
console.log("Remove first 25:", removeFirstK(originalString, 25));
Original: JavaScript Programming Remove first 4: Script Programming Remove first 10: Programming Remove first 25:
Method 3: Using substring() Method
The substring() method returns characters between two indices in a string.
const removeKChars = (str, k) => {
return str.substring(k);
};
const text = "Hello World";
console.log("Original:", text);
console.log("Remove first 6:", removeKChars(text, 6));
console.log("Remove first 2:", removeKChars(text, 2));
Original: Hello World Remove first 6: World Remove first 2: llo World
Comparison of Methods
| Method | Support | Negative Index | Recommended |
|---|---|---|---|
substr() |
Deprecated | Yes | No |
slice() |
Modern | Yes | Yes |
substring() |
Standard | No (treats as 0) | Yes |
Edge Cases
Here's how different methods handle edge cases:
const testString = "Test";
// When k is greater than string length
console.log("slice(10):", testString.slice(10)); // ""
console.log("substring(10):", testString.substring(10)); // ""
// When k is 0
console.log("slice(0):", testString.slice(0)); // "Test"
console.log("substring(0):", testString.substring(0)); // "Test"
// When k equals string length
console.log("slice(4):", testString.slice(4)); // ""
console.log("substring(4):", testString.substring(4)); // ""
slice(10): substring(10): slice(0): Test substring(0): Test slice(4): substring(4):
Conclusion
Use slice() as the preferred method for removing first k characters from strings. Avoid substr() as it's deprecated, and consider substring() for basic use cases.
Advertisements
