Monday, 13 April 2015

String Methods (String Session-4)

String Concatenation in Java

In java, string concatenation forms a new string that is the combination of multiple strings. There are two ways to concat string in java:
  1. By + (string concatenation) operator
  2. By concat() method

1) String Concatenation by + (string concatenation) operator

Java string concatenation operator (+) is used to add strings. For Example:
1.    class TestStringConcatenation1{  
2.     public static void main(String args[]){  
3.       String s="Sachin"+" Tendulkar";  
4.       System.out.println(s);//Sachin Tendulkar  
5.     }  
6.    }  
Output:Sachin Tendulkar
The Java compiler transforms above code to this:
1.    String s=(new StringBuilder()).append("Sachin").append(" Tendulkar).toString();  
In java, String concatenation is implemented through the StringBuilder (or StringBuffer) class and its append method. String concatenation operator produces a new string by appending the second operand onto the end of the first operand. The string concatenation operator can concat not only string but primitive values also. For Example:
1.    class TestStringConcatenation2{  
2.     public static void main(String args[]){  
3.       String s=50+30+"Sachin"+40+40;  
4.       System.out.println(s);//80Sachin4040  
5.     }  
6.    }  
80Sachin4040

Note: After a string literal, all the + will be treated as string concatenation operator.

2) String Concatenation by concat() method

The String concat() method concatenates the specified string to the end of current string. Syntax:
1.    public String concat(String another)  
Let's see the example of String concat() method.
1.    class TestStringConcatenation3{  
2.     public static void main(String args[]){  
3.       String s1="Sachin ";  
4.       String s2="Tendulkar";  
5.       String s3=s1.concat(s2);  
6.       System.out.println(s3);//Sachin Tendulkar  
7.      }  
8.    }  
Sachin Tendulkar

Substring in Java

A part of string is called substring. In other words, substring is a subset of another string. In case of substring startIndex is inclusive and endIndex is exclusive.

Note: Index starts from 0.

You can get substring from the given string object by one of the two methods:
  1. public String substring(int startIndex): This method returns new String object containing the substring of the given string from specified startIndex (inclusive).
  2. public String substring(int startIndex, int endIndex): This method returns new String object containing the substring of the given string from specified startIndex to endIndex.
In case of string:
  • startIndex: inclusive
  • endIndex: exclusive
Let's understand the startIndex and endIndex by the code given below.
1.    String s="hello";  
2.    System.out.println(s.substring(0,2));//he  
In the above substring, 0 points to h but 2 points to e (because end index is exclusive).

Example of java substring

1.    public class TestSubstring{  
2.     public static void main(String args[]){  
3.       String s="Sachin Tendulkar";  
4.       System.out.println(s.substring(6));//Tendulkar  
5.       System.out.println(s.substring(0,6));//Sachin  
6.     }  
7.    }  
Tendulkar
Sachin


Java String class methods

The java.lang.String class provides a lot of methods to work on string. By the help of these methods, we can perform operations on string such as trimming, concatenating, converting, comparing, replacing strings etc.
Java String is a powerful concept because everything is treated as a string if you submit any form in window based, web based or mobile application.
Let's see the important methods of String class.

Java String toUpperCase() and toLowerCase() method

The java string toUpperCase() method converts this string into uppercase letter and string toLowerCase() method into lowercase letter.
1.    String s="Sachin";  
2.    System.out.println(s.toUpperCase());//SACHIN  
3.    System.out.println(s.toLowerCase());//sachin  
4.    System.out.println(s);//Sachin(no change in original)  
SACHIN
sachin
Sachin

Java String trim() method

The string trim() method eliminates white spaces before and after string.
1.    String s="  Sachin  ";  
2.    System.out.println(s);//  Sachin    
3.    System.out.println(s.trim());//Sachin  
  Sachin  
Sachin

Java String startsWith() and endsWith() method

1.    String s="Sachin";  
2.     System.out.println(s.startsWith("Sa"));//true  
3.     System.out.println(s.endsWith("n"));//true  
true
true

Java String charAt() method

The string charAt() method returns a character at specified index.
1.    String s="Sachin";  
2.    System.out.println(s.charAt(0));//S  
3.    System.out.println(s.charAt(3));//h  
S
h

Java String length() method

The string length() method returns length of the string.
1.    String s="Sachin";  
2.    System.out.println(s.length());//6  
6

Java String intern() method

A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
1.    String s=new String("Sachin");  
2.    String s2=s.intern();  
3.    System.out.println(s2);//Sachin  
Sachin

Java String valueOf() method

The string valueOf() method coverts given type such as int, long, float, double, boolean, char and char array into string.
1.    int a=10;  
2.    String s=String.valueOf(a);  
3.    System.out.println(s+10);  
Output:
1010

Java String replace() method

The string replace() method replaces all occurrence of first sequence of character with second sequence of character.
1.    String s1="Java is a programming language. Java is a platform. Java is an Island.";    
2.    String replaceString=s1.replace("Java","Kava");//replaces all occurrences of "Java" to "Kava"    
3.    System.out.println(replaceString);    
Output:
Kava is a programming language. Kava is a platform. Kava is an Island.


No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...