[an error occurred while processing this directive]

Taking  Substring

                         

In this example we are taking a sub string from a given string.

In this example we are creating an string object .We initialize this string object as "Rajesh Kumar". We are taking sub string by use of substring()  method.

The methods used:
substring(int i):
This method is used to find all sub string after index i.

substring(int start,int end):
This is used to find the substring between start and end point.

The code of the program is given below:

public class SubstringExample1{
    public static void main(String[] args){
        String string = "Rajesh kumar"
        System.out.println("String : " + string);
        String substring = string.substring(3);
        System.out.println("String after 3rd index:
 " 
+ substring);   
        substring = string.substring(12);
        System.out.println("Substring (1,2): " 
substring
);
    }
}

The output of the program is given below:

C:\convert\rajesh\completed>javac SubstringExample1.java
C:\convert\rajesh\completed>java SubstringExample1
String : Rajesh kumar
String after 3rd index: esh kumar
Substring (1,2): a

Download this example.

                         

[an error occurred while processing this directive]