JavaScript to keep only first N characters in a string
Given a string and the task is to keep only first n characters of the string. There are various methods to solve this problem in JavaScript, some of them are discussed below:
Method 1: Using substring() Method: The string.substring() method is an inbuilt function in JavaScript which is used to return the part of the given string from start index to end index. Indexing starts from zero (0).
Syntax:
string.substring(Startindex, Endindex)
Below program demonstrates the above approach.
Example:
<script> // JavaScript to keep only first // 'n' characters of String // Original string var str = "GeeksforGeeks"; // Keep first 5 letters var n = 5; document.write("Original String = \"" + str + "\"<br>"); document.write("n = " + n + "<br>"); // Using substring() method str = str.substring(0, n); document.write("Keep first " + n + " characters of original String = \"" + str + "\"<br>"); </script> |
Output:
Original String = "GeeksforGeeks" n = 5 Keep first 5 characters of original String = "Geeks"
Method 2: Using slice() Method: The string.slice() is an inbuilt function in javascript which is used to return a part or slice of the given input string.
Syntax:
string.slice(startingindex, endingindex)
Below program demonstrates the above approach.
Example:
<script> // JavaScript script to keep only // first 'n' characters of String // Original string var str = "Data Structure"; // Keep first 11 letters var n = 11; document.write("Original String = \"" + str + "\"<br>"); document.write("n = " + n + "<br>"); // Using slice() method str = str.slice(0, n); document.write("Keep first " + n + " characters of original String = \"" + str + "\"<br>"); </script> |
Output:
Original String = "Data Structure" n = 11 Keep first 11 characters of original String = "Data Struct"
Recommended Posts:
- JavaScript | Strip all non-numeric characters from string
- Replace special characters in a string with underscore (_) in JavaScript
- How to convert special characters to HTML in Javascript?
- How to get the last n characters of a PHP string?
- PHP | Different characters in the given string
- JavaScript | Check if a string is a valid JSON string
- JavaScript | Difference between String.slice and String.substring
- Iterate over characters of a string in Python
- How to remove all non-printable characters in a string in PHP?
- How to count string occurrence in string using JavaScript?
- JavaScript | Insert a string at position X of another string
- Convert a String to a List of Characters in Java
- How to check a string begins with some given characters/pattern ?
- Convert List of Characters to String in Java
- How to get nth occurrence of a string in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



