Java String substring Example
String class substring method example
String class substring method example:- This example demonstrates the working of substring method example. this method returns a substring of a String , here substring is a new string in which value from certain index passed has been copied and pasted of String under method process
Syntax:- substring(int beginIndex)
Here is the code:-
/**
* @(#) SubstringString.java
* SubstringString class demonstrates the working of substring() method of String class of lang package
* @version 16-May-2008
* @author Rose India Team
*/
public class SubstringString {
public static void main(String args[]) {
String str = "as per einstein conclusion nothing can travel faster than light", str1 = " ", str2 = "he ram";
str1 = str.substring(0);
// method substring takes the complete data from specified index
// location of the string under taken in to the String in which the
// method is invoked
// complete value of string str has been copied as zero(0) index
// location has been passed inside method parentheses
System.out.println("value of String str1 taken by the method : "
+ str1);
System.out.println("value String str2 taken by the method: "
+ str2.substring(3));
}
}
|
Output of the program:-
value of String str1 taken by the method : as per einstein conclusion nothing can travel faster than light
value String str2 taken by the method: ram |
|