Monday, 7 May 2012

Java String equals 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
String class equals method example

String class equals method example:- This method demonstrates the working of equals() method. 
it returns boolean values true and false after examining the two objects taken under checking process

Syntax:-equals(Object anObject) 


Here is the code:-

  /**
  * @(#) EqualsString.java
  * EqualsStringString class demonstrates the working of equals() method of String class of lang package
  */  




public class EqualsString {
  public static void main(String args[]) {

    Integer in = new Integer(12234);
    String str = new String("12234");
    
    // since both objects posses same value but the type of value is
    // different. Here there are two types of objects compared one is
    // Integer type and the other one is String type so the method returns
    // false as values types didnot match
    
    System.out.println( "value types don't matches: " + in.equals(str));
             

    // method returns true if and only if none of the Strings compared
    // posses null value and also must that there value matches each other
    // in sequence, cases and all aspects of equality including value types
    String str1 = new String("  hare krishna hare rama! rama rama hare hare");
    String str2 = str1.substring(0);
    String str3 = " ", str4 = " ";
    String str5 = "heram", str6 = "Heram";
    //Method returns true here as both Strings str1 and str2 posses same values
    System.out.println(" 'true' is returned:  " + str1.equals(str2));
    //Both the String mentioned above don't have any value but still Strings are equal as both are null type objects compared"             
    System.out.println("Method returns true here:  " + str3.equals(str4));
    //Method here returns false because values of Strings did not match when their respective letters case are concerned        
    System.out.println(" 'false' is returned:  " + str5.equals(str6));
            
  }
}
Output of the program:-
--------------------------------
value types don't matches: false

'true' is returned:  true

Method returns true here:  true

'false' is returned:  false

Java String format(String format)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
String class format(String format) method example

String class format(String format) method example. This example shows you how to use format(String format) method.This method  Returns a formatted string using the specified format string and arguments.

  


Here is the code:-
/* 
 * @(#) Formatstring1.java
 *Program  Returns a formatted string using the specified format string and arguments.
 */


import java.util.*;
public class Formatstring1
  {    
    public static void main(String[] args)
    {
      long n = 461012;
    //Returns a formatted string using the specified format string and arguments.
      System.out.format("%d%n", n); 
      System.out.format("%08d%n", n);
     
    }
}

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

Java String matches 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
38
39
40
41
42
String class matches method example

String class matches method example:- This example demonstrates the working of matches() method. 
this method returns boolean values if it gets same match of object value to the passes argument value in the parentheses.

Syntax:-matches(String regex) 


Here is the code:-
/**
 * @(#) MatchesString.java 
 * MatchesString class demonstrates the working of matches() method of String class of lang package
 * 
 */


public class MatchesString {
  public static void main(String args[]) {

    // method just matches the String value to the specified value in the
    // parentheses and generates boolean values
    String str = " heram", str1 = "xaaaaa";
    boolean result = str1.matches("aaaaa");
    // method not get the same values therefore it returns false
    System.out.println("Method returns 'false':  " + result);

    // method gets the same values but not able to match there allignments
    // therefore it returns false
    result = str.matches("heram");
    System.out.println("Method returns 'false':  " + result);

    // method gets the same values with precise allignments therefore it
    // returns true
    result = str.matches(" heram");
    System.out.println("Method returns 'true':  " + result);
  }
}
Output of the program:-
------------------------------
Method returns 'false':  false
Method returns 'false':  false
Method returns 'true':  true

String Array 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
String Array example. This example shows you how to initialize String Array

This example shows you how to initialize String Array. 


Here is the code

/**
 * @ # StringArray.java
 * A class repersenting how initialization String Arrays 
 * 
 */
public class StringArray {

    public static void main(String args[]) {

        //initialization String Arrays
        String arr[] = {"Zero", "One", "Three"};

        System.out.println("String Array arr ...");
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        
        String arr1[] = new String[]{"Zero", "One", "Two"};
        System.out.println("\nString Array arr1...");
        for (int i = 0; i < arr1.length; i++) {
            System.out.println(arr1[i]);
        }

        // This allocates the array but they are all null
        String arr2[] = new String[3];

        // here you start to stuff in the values.
        arr2[0] = "Zero";
        arr2[1] = "One";
        arr2[2] = "Two";

        System.out.println("\nString Array arr2...");
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }
    }
}
Output :
--------------------
String Array arr ...
Zero
One
Three

String Array arr1...
Zero
One
Two

String Array arr2...
Zero
One
Two

Java String contains 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
String class Contains method example

String class Contains method example:- 
This example demonstrates the working of contains() method. 
this method generates boolean value true if and only if it finds the same sequence of data in the called String.

Syntax:-contains(CharSequence s) 


Here is the code:-

  /**
  * @(#) ContainsString.java
  * ContainsString class demonstrates the working of contains() method of String class of lang package
  *
  */  


public class ContainsString {
  public static void main(String args[]) {

    // contains() method checks the sequence characters and
    // number or both characters and numbers together passed in parentheses
    // into the called String
    String str = "om namah shivaya", str1 = "420_9211", str2, str3;

    CharSequence char0 = "am", char1 = "_";
    boolean result = str.contains(char0);

    // Method ables to find the sequence of characters(am) therefore it
    // generates boolean type true
    System.out.println("Method returns true here:  " + "'" + result + "'");
    result = str1.contains(char1);
    System.out.println();
    // Method ables to fing the symbol underscore(_) therefore it generates
    // boolean type true"
    System.out.println("'true' is returned:    " + "'" + result + "'");

    System.out.println();
    str2 = " hare ram1 hare ram, hare krishna hare ram ";
    System.out.println("'false' is returned:    " + "'"
        + str2.contains("krishna ram1") + "'");

  }

}
Output of the program:-
----------------------------------
Method returns true here:  'true'

'true' is returned:    'true'

'false' is returned:    'false'

Java String indexOf 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
38
Java String indexOf Example

String class indexOf method example

String class indexOf method example:- This example demonstrates the working of indexOf method. this method returns the index location of character specified.

Syntax:-indexOf('h') 


Here is the code:-

  /**
  * @(#) IndexOfString.java
  * IndexOfString class demonstrates the working of indexOf() method of String class of lang package
  * 
  */  


public class IndexOfString {
  public static void main(String args[]){
    
    // Method here returns index location or number position of the specified character.         
    String str = "hare krishna hare krishna krishna krishna hare hare";
    int prabhu = str.indexOf('h'), heram;
    
    System.out.println("Index of 'h' in String 'str' is: " + prabhu);    
    System.out.println("Index of 'k' in String 'str' is: " + str.indexOf("k"));
        
    //For a word or sequence of charaters, method returns the index location of letter with which the word starts.
    System.out.println("Index of 'krishna' in String 'str' is: " + str.indexOf("krishna"));
      
  }
}
Output of the program:-
-----------------------
Index of 'h' in String 'str' is: 0
Index of 'k' in String 'str' is: 5
Index of 'krishna' in String 'str' is: 5
Related Posts Plugin for WordPress, Blogger...