You are here:   HomeJava ProgrammingJava ArrayString substring() in Java Example
by Dinesh Thakur Category: array

Programmers often need to retrieve an individual character or a group of characters (substring) from a string. For example: In a word processing program, when a part of string is copied or deleted. While charAt () method returns only a single character from a string, the substring () method in the String class can be used to obtain a substring from a string.

String substring(int beginIndex) : This method returns a new string that is a substring of the invoking string. The substring returned contains a copy of characters beginning from the specified index beginIndex and extends to the end of the string. For example,

S1.substring(3); // returns substring 'come'

String substring (int beginIndex, int endIndex) : This method is another version of previous method. It also returns a substring that begins at the specified begin Index and extends to the character at index endIndex-1. For example,

s1.substring(3,6); // returns substring "come".

public class SubstringMethods
     public static void main(String[] args)
      String sl = "Welcome" ;
      System.out.println("sl.substring(3) = " + sl.substring(3));
      System.out.println("substring(3,6) = " + sl.substring(3));

String substring() in Java Example

class StrSubString

            public static void main(String args[])
                        String k="Hello Dinesh";
                        String m="";
                        m=k.substring (6,12);

String substring() in Java Example

Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.

What's New and Popular

Popular Article