C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript String substr() MethodThe JavaScript string substr() method fetch the part of the given string and return the new string. The number of characters to be fetched depends upon the length provided with the method. This method doesn't make any change in the original string. SyntaxThe substr() method is represented by the following syntax: string.substr(start,length) Parameterstart - It represents the position of the string from where the fetching starts. length - It is optional. It represents the number of characters to fetch. ReturnPart of the string. JavaScript String substr() Method ExampleLet's see some simple examples of substr() method. Example 1Let's see a simple example to print the part of the string. <script> var str="TheDeveloperBlog"; document.writeln(str.substr(0,4)); </script> Output: Java Example 2Let's see one more example to print the part of the string. <script> var str="TheDeveloperBlog"; document.writeln(str.substr(5,5)); </script> Output: point Example 3In this example, we will provide only the starting index. <script> var str="TheDeveloperBlog"; document.writeln(str.substr(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.substr()); </script> Output: TheDeveloperBlog Example 5In this example, we will provide negative number as a starting index. In such case, the method starts fetching from the end of the string. <script> var str="TheDeveloperBlog"; document.writeln(str.substr(-5,5)); </script> Output: point
Next TopicJavaScript String
|