String in Java

String in Java 

 

                 String is a sequence of characters.But in Java, a string is an object that represents a sequence of characters.

There are two ways to create a String object:

1)String literal : Java String literal is created by  using double quotes.
 
For Example: String str=Hello String;

2)new keyword : Java String is created by using a keyword “new”.

For example: String str=new String(Hello String);  

It will creates two objects in String pool and in heap and one reference variable where the variable ‘str’ will refer to the object in the heap.



String's Methods.

 

1) String length() : The Java String length() method tells the length of the string. It returns count of total number of characters present in the String.

example :        

String str="nirav patel on java";

System.out.println(str.length());

 

 

2) String concat() : The Java String concat() method combines a specific string at the end of another string and ultimately returns a combined string. It is like appending another string.

 example :        

   String str="hello java";

   str=str.concat("how are you");

   System.out.println(str);

 

3)String compareTo() : The Java String compareTo() method compares the given string with current string. It is a method of ‘Comparable’ interface which is implemented by String class. Don’t worry, we will be learning about String interfaces later. It either returns positive number, negative number or 0
 

example :     

  String name="niravpatel";

  String name="niravpatel";

  System.out.println(name.compareTo(name)); 

  // result is 0 because both are string are same. 

 

 4) String IsEmpty() : This method checks whether the String contains anything or not. If the java String is Empty .it returns true else false.

 example :       

 String name = "nirav";

 System.out.println(name.isEmpty()); //false

 

5) String Trim() :  string trim() method removes the leading and trailing spaces. It checks the unicode value of space character (‘\u0020’) before and after the string. If it exists, then removes the spaces and return the omitted string.

example :      

 String name = "   nirav  ";

  System.out.println(name + " hello"); 

  //result is  : niravhello


6) String toLowerCase() : The java string toLowerCase() method converts all the characters of the String to lower case. For example:

example :      

 String name = "NIRAV";

  System.out.println(name.toLowerCase()); 

  //result is  :nirav

 

7) String toUpperCase() : The java string toUpperCase() method converts all the characters of the String to upper case. For example:

example :       

 String name = "nirav";

  System.out.println(name.toUpperCase()); 

  //result is  :NIRAV

 

 

8) String replace() : String replace() method returns a string, replacing all the old characters or Char Sequence to new characters. There are 2 ways to replace methods in a Java String. 

example :        

String name = "nirav patel";

System.out.println(name.replace("nirav","niravkumar")); 

//result is  :niravkumar patel



And many more methods are there..

Thank you...

 

 

 



 


Comments

Popular posts from this blog

Java LinkedList class in Collection

Java ArrayList class in collections

Constructors in Java