Tuesday, November 19, 2013

String class 02 - Special methods of String class



In String class there are set of methods. But we are talking about few special methods of String class.

length()

In this method you can find the number of characters in your String. It returns the number of characters in a String. 

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

          String s1 = "chevrolet";
          String s2 = "CHEVrolet_1234#$&--";
  
          System.out.println(s1.length());
          System.out.println(s2.length());
      }
}


charAt()

This method is used to find the char at position you want. In this method, you have to provide the index as parameter. Then it will return the character as to the mentioned index.

public class MethodTest {
      public static void main(String[] args) {
          String s1 = "chevrolet";
          String s2 = "CHEVrolet_1234#$&--";
  
          System.out.println(s1.charAt(5));
          System.out.println(s2.charAt(85));
      }
}

When you compile this program it will return a character and then a exception(java.lang.StringIndexOutOfBoundsException). We talk about exceptions in later posts. It is very easy to understand this exception, String index out of bounds. You can understand about this only thinking about the meaning of this. There are only 23 characters in String s2. So charAt() method is asking for a index out of bounds.


substring()

This method is used to return a part of a String . In this method you can pass only beginning index  or both beginning and ending indexes. Try following example and carefully look about the outputs(Think about ending index and output).

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

          String s1 = "0123456789";
  
          System.out.println(s1.substring(2));
          System.out.println(s1.substring(2,5));
      }
}


concat()

This method is used to join Strings.

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

          String s1 = "easy";
          String s2 = "java";
  
          System.out.println(s1.concat(s2));
          System.out.println(s2.concat("se"));
          System.out.println(s1.concat(s2).concat("se"));
      }
}


replace()

This method is used to replace any character in a String. In this method you have to use old character as first parameter and new character as second parameter.

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

          String s1 = "block";
  
          System.out.println(s1);
          System.out.println(s1.replace('b','c'));
          System.out.println(s1);
      }
}



trim()

This method is used to remove white spaces at both ends in a String. But if there are white spaces in middle, it does not affect.

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

          String s1 = "      This is ";
          String s2 = "Nokia Lumia      ";
          String s3 = "1020";
  
          System.out.println(s1 + s2 + s3);
          System.out.println(s1.trim() + s2.trim() + s3.trim() );   
      }
}


indexOf()


This method is used to return the index of provided character of a String. You can find the indexes of character or word. If there are no choice it will return -1.

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

          String s1 = "Chevrolet Tahoe is amazing";

          System.out.println(s1.indexOf("v")); //there is one 'v' in s1
          System.out.println(s1.indexOf("e")); //there are 3 'e's in s1
          System.out.println(s1.indexOf('e',3));//start position is 3
          System.out.println(s1.indexOf("x")); //x is not in s1
          System.out.println(s1.indexOf(' ')); //find the index of a space  
          System.out.println(s1.indexOf("is"));//search the index of a word
          System.out.println(s1.lastIndexOf('e'));//fine the last index
      }    
}

equals()

In this method it will return a Boolean output(true or false). This method is used to check equality of  Strings. In this method there is a special method called equalsIgnoreCase(). This can be used if you want to skip case sensitive. 

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

          String s1 = "Nokia";
          String s2 = "Nokia";
          String s3 = "nokia";
          String s4 = "Lumia";

          System.out.println(s1.equals(s2));
          System.out.println(s1.equals(s3));
          System.out.println(s1.equals(s4));
          System.out.println(s1.equalsIgnoreCase(s2));
      }
}


toUpperCase() / toLowerCase()

This is very simple to understand. It changes the characters lower to upper or upper to lower.

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

          String s1 = "NOKIA";
          String s2 = "Nokia";

          System.out.println(s1.toLowerCase());
          System.out.println(s2.toUpperCase());
     }
}


compareTo()  

This method is complicated. You have to refer the example to understand this thing more clearly.In this method you have to think about first letter, number of characters, if first letter is same you have to think about second letter so on.


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

          String s1 = "apple";
          String s2 = "Apple";
          String s3 = "grape";
          String s4 = "apples";
          String s5 = "axxxx";

          System.out.println(s1.compareTo(s1)); //apple=apple
          System.out.println(s1.compareToIgnoreCase(s2)); 
 
          System.out.println(s1.compareTo(s3)); 
          System.out.println(s1.compareTo(s4)); 
          System.out.println(s1.compareTo(s5));
          
          System.out.println(s3.compareTo(s1));
          System.out.println(s3.compareTo(s2));
          System.out.println(s3.compareTo(s4));
          

      }
}


toString()

This method is used to covert number to String. There are 3 types to convert a number to a String. This method is used as one type of them.

public class MethodTest {
      public static void main(String[] args) {
          Integer x = 100; //Integer is a wrapper class
          String s = x.toString();
  
          System.out.println(s);
          System.out.println(x + 200);
          System.out.println(s + 200);
       }
}

Chained methods


This is a statement with chain of a method(collection of methods). Try following example to understand about it. Any time left method  is invoked the second method.


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

           String s = "Apples are red";
           String p = s.toLowerCase().concat(" and green");
  
           System.out.println("Original String: " + s);
           System.out.println("Edited String: " + p);
     }
}