

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use the substring() method of java.lang.String class in Java?
<p>The <strong>substring()</strong> method returns a String datatype which corresponds to the original String starting from the begin index until the end Index. If the end index is not specified, it is imperative that the <strong>endIndex </strong>is the String length. Since we are dealing with the String, the index starts at '<strong>0'</strong> <strong>position</strong>.</p><h2>Syntax</h2><pre class="prettyprint notranslate">public String substring(int beginIndex) public String substring(int beginIndex, int endIndex)</pre><p><strong>beginIndex:</strong> the starting index or position where we want to start to cut or substring our String.</p><p><strong>endIndex: </strong> the end index or position where we want to end our cutting or substring our String.</p><p>This method<strong> returns a String datatype </strong>which corresponds to the part of our string which we cut. If no <strong>endIndex </strong>is specified, then the end index is assumed to be<strong> String length -1</strong> and an <strong>IndexOutOfBoundsException </strong>is thrown if the <strong>beginIndex </strong><strong>is negative</strong> or it is <strong>larger than the length of the String.</strong></p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/9zVxBW " rel="nofollow" target="_blank">Live Demo</a></p><pre class="prettyprint notranslate" style="">public class StringSubstringTest{ public static void main(String[] args) { String str = "Welcome to Tutorials Point"; System.out.println(str.substring(5)); System.out.println(str.substring(2, 5)); str.substring(6); System.out.println("str value: "+ str); String str1 = str.substring(5); System.out.println("str1 value: "+ str1); } }</pre><h2>Output</h2><pre class="result notranslate">me to Tutorials Point lco str value: Welcome to Tutorials Point str1 value: me to Tutorials Point</pre>
- Related Questions & Answers
- How to use Java substring Method?
- Java String substring() Method example.
- How to use headSet() method of Java NavigableSet Class
- How to use subSet() method of Java NavigableSet Class
- When can we use intern() method of String class in Java?
- How to use isAlive() method of Thread class in Java?
- Java lang Integer.toHexString() Method with Examples
- Java lang Long.toOctalString() Method with Examples
- How to use Java string replace method?
- How to use the Compare method of the string class in C#?
- Java substring() method example.
- Explain the usage of the split() method of the String class in Java.
- Explain the usage of the valueOf() method of the String class in Java
- The :lang Pseudo-class in CSS
- How to use file class in Java?
Advertisements