Monday, 7 May 2012

Java String substring Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
 */


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

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...