Search This Blog

Saturday, 13 December 2014

String use full methods

Most useful string method in general programming

In programming, we can use this most useful String method.

1. charAt()
  • This method return string character, input position of character in method and return character.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Main {
 public static void main(String args[]) { 
  
  /**
   * charAt method
   */
  String string="Hello india, java is best programming language";
  System.out.println("Char at="+string.charAt(0));  
 }
}
Output
Char at=H










    2. compareTo(object)
  • This method use for compare string to object

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class Main {
 public static void main(String args[]) {
  /**
   * Compare method
   * The value 0 if the argument is a string lexicographically equal to this string; 
   * a value less than 0 if the argument is a string lexicographically greater than this string; 
   * and a value greater than 0 if the argument is a string lexicographically less than this string.
   */

  String compare_1="hello";
  String compare_2="hello";
  System.out.println("compare="+compare_1.compareTo(compare_2));

 }
}

Output
compare=0