Saturday, May 11, 2013

Java String

Java String

String class is available under the package java.lang.String.

String class is very important class, we almost use most of the places while developing the applications.

We no need to import this class in our code, automatically this class is available.

String is final class, so the string object can not be modify.

String class is immutable object, that means its contents can not be editable.

There are lot of methods are there to perform string operations like indexOf, concat,charAt...etc

We can create string objects in number of ways.

String str = "Javademos.blogspot";

It is a string constant, it will store in string constant pool.

The other way to create string object is by using new operator.

String str = new String("Javademos.blogspot");

char[] cArray = {'J','A','V','A'};

String str = new String(cArray);

There are many ways we can create string object like passing the byte[] constructor...etc.

We have  number of methods in string class to perform operations on strings.

I am giving the most useful methods which are frequently using in java real time applications.

 public class StringExample {  
      public static void main(String[] args) {  
                String str1 = new String("java");  
                String str2 = new String("j2ee");  
                //equals method Compares string to the specified object.  
                boolean flag = str1.equals(str2);  
                System.out.println("equals .. "+flag);  
                //equalsIgnoreCase method Compares String to another String, ignoring case considerations.  
                flag = str1.equalsIgnoreCase(str2);  
                System.out.println("equalsIgnoreCase .. "+flag);  
                //charAt method gives the character position.  
                int position = str1.charAt(3);  
                System.out.println("charAt .. "+position);  
                //concat the strings  
                String str = str1.concat(str2);  
                System.out.println("concat .. "+str);  
                //replace the characters  
                str = str1.replace('a','b');  
                System.out.println("replace .. "+str);  
                //return the substring  
                str = str1.substring(2);  
                System.out.println("substring .. "+str);  
                //convert all the chars to lower case  
                str = str1.toLowerCase();  
                System.out.println("toLowerCase .. "+str);  
                //convert all the chars to upper case  
                str = str1.toUpperCase();  
                System.out.println("toUpperCase .. "+str);  
                String str4 = " abc ";  
                //with leading and trailing whitespace omitted  
                str = str4.trim();  
                System.out.println("trim .. "+str);  
                //Tests if this string starts with the specified prefix  
                flag = str1.startsWith("j");  
                System.out.println("startsWith .. "+flag);  
                //Tests if this string ends with the specified prefix  
                flag = str1.endsWith("j");  
                System.out.println("endsWith .. "+flag);  
                //length of the string  
                System.out.println("endsWith .. "+str1.length());  
      }  
 }  


OutPut : equals .. false
equalsIgnoreCase .. false
charAt .. 97
concat .. javaj2ee
replace .. jbvb
substring .. va
toLowerCase .. java
toUpperCase .. JAVA
trim .. abc
startsWith .. true
endsWith .. false
endsWith .. 4

There are lot of methods are available you can refer the java string api.

1 comment :

  1. This blog is really informative i really had fun reading it.

    ReplyDelete

 
Disclaimer : If you find any mistakes / corrections / feedback Please let us know...This website author shall not accept liability or responsibility for any errors, mistakes or misstatements found in this website.