JavaScript to keep only first N characters in a string
In this article, we have given a string and the task is to keep only the first n characters of the string using JavaScript. There are various methods to solve this problem, some of which are discussed below:
Methods to keep only First N characters in a String:
Table of Content
- Using substring() Method
- Using slice() Method
- Using JavaScript For loop
- Using String’s substr() Method
Method 1: Using substring() Method
The string.substring() method is used to return the part of the given string from the start index to the end index. Indexing starts from zero (0).
Syntax:
string.substring(Startindex, Endindex)
Example: This example uses the above-explained approach.
// JavaScript to keep only first
// 'n' characters of String
// Original string
let str = "w3wiki";
// Keep first 5 letters
let n = 5;
console.log("Original String = " + str);
console.log("n = " + n);
// Using substring() method
str = str.substring(0, n);
console.log("Keep first " + n +
" characters of original String = " + str);
Output
Original String = w3wiki n = 5 Keep first 5 characters of original String = Beginner
Method 2: Using slice() Method
The string.slice() method is used to return a part or slice of the given input string.
Syntax:
string.slice(startingindex, endingindex)
Example: This example uses slice() method to get part of a string.
// JavaScript script to keep only
// first 'n' characters of String
// Original string
let str = "Data Structure";
// Keep first 11 letters
let n = 11;
console.log("Original String = " + str);
console.log("n = " + n);
// Using slice() method
str = str.slice(0, n);
console.log("Keep first " + n +
" characters of original String = " + str);
Output
Original String = Data Structure n = 11 Keep first 11 characters of original String = Data Struct
Method 3: Using JavaScript For loop
Looping in programming languages is a feature that facilitates the execution of a set of instructions repeatedly until some condition evaluates and becomes false. We come across for loop which provides a brief and systematic way of writing the loop structure.
Syntax:
for (statement 1 ; statement 2 ; statement 3){
code here...
}
Example: In this example, we will loop till the given index and store characters in the result string.
// JavaScript to keep only first
// 'n' characters of String
// Original string
let str = "w3wiki";
// Keep first 5 letters
let n = 5;
let res = "";
console.log("Original String = " + str);
console.log("n = " + n);
// Loop till given number of characters
for (let i = 0; i < n; i++) {
res += str[i];
}
console.log(
"Keep first " + n +
" characters of original String = "
+ res
);
Output
Original String = w3wiki n = 5 Keep first 5 characters of original String = Beginner
Method 4: Using String’s substr() Method
The substr() method is used to extract parts of a string, beginning at the specified start position, and returns the specified number of characters.
Syntax:
string.substr(startIndex, length)
Example:
// JavaScript to keep only first
// 'n' characters of String
// Original string
let str = "Hello World";
// Keep first 5 characters
let n = 5;
console.log("Original String = " + str);
console.log("n = " + n);
// Using substr() method
str = str.substr(0, n);
console.log("Keep first " + n +
" characters of original String = " + str);
Output
Original String = Hello World n = 5 Keep first 5 characters of original String = Hello
Method 5: Using Array.prototype.join() with Array.from()
The Array.from() method creates a new, shallow-copied Array instance from an array-like or iterable object. By converting the string into an array of characters, we can easily manipulate and then join the desired portion back into a string.
Example: This example demonstrates using Array.from() to convert the string to an array of characters, then slicing and joining them to keep only the first n characters.
// JavaScript to keep only first 'n' characters of String
// Original string
let str = "OpenAI GPT-4";
// Keep first 8 characters
let n = 8;
console.log("Original String = " + str);
console.log("n = " + n);
// Using Array.from() and join() method
str = Array.from(str).slice(0, n).join('');
console.log("Keep first " + n + " characters of original String = " + str);
Output
Original String = OpenAI GPT-4 n = 8 Keep first 8 characters of original String = OpenAI G