TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript String substring() Method

JavaScript string substring() Method with example, by string literal, by string object (using new keyword), javascript string methods, charAt(index) method, concat(str) method, indexOf(str) method, lastIndexOf(str) method, lowerCase() method, upperCase() method, slice(beginIndex, endIndex) method, trim() method, toLowerCase(), slice(), concat(), charCodeAt() method etc.

<< Back to JAVASCRIPT

JavaScript String substring() Method

The 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.

Syntax

The substring() method is represented by the following syntax:

string.substring(start,end)

Parameter

start - 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.

Return

Part of the string.

JavaScript String substring() Method Example

Let's see some simple examples of substring() method.

Example 1

Let's see a simple example to print the part of the string.

<script>
var str="TheDeveloperBlog";
document.writeln(str.substring(0,4));
</script>
Test it Now

Output:

Java

Example 2

In this example, we will fetch single character from the string.

<script>
var str="TheDeveloperBlog";
document.writeln(str.substring(4,5));
</script>
Test it Now

Output:

t

Example 3

In this example, we will provide only the starting index.

<script>
var str="TheDeveloperBlog";
document.writeln(str.substring(5));
</script>
Test it Now

Output:

point

Example 4

In 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>
Test it Now

Output:

TheDeveloperBlog

Next TopicJavaScript String




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf