C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript String substring() MethodThe JavaScript string substring() method fetch the string on the basis of provided index and returns the new sub string. It works similar to the slice() method with a difference that it doesn't accepts negative indexes. This method doesn't make any change in the original string. SyntaxThe substring() method is represented by the following syntax: string.substring(start,end) Parameterstart - It represents the position of the string from where the fetching starts. end - It is optional. It represents the position up to which the string fetches. ReturnPart of the string. JavaScript String substring() Method ExampleLet's see some simple examples of substring() method. Example 1Let's see a simple example to print the part of the string. <script> var str="TheDeveloperBlog"; document.writeln(str.substring(0,4)); </script> Output: Java Example 2In this example, we will fetch single character from the string. <script> var str="TheDeveloperBlog"; document.writeln(str.substring(4,5)); </script> Output: t Example 3In this example, we will provide only the starting index. <script> var str="TheDeveloperBlog"; document.writeln(str.substring(5)); </script> Output: point Example 4In this example, we will not pass any parameter with the method. In such case, the method returns the complete string. <script> var str="TheDeveloperBlog"; document.writeln(str.substring()); </script> Output: TheDeveloperBlog
Next TopicJavaScript String
|