Saturday 31 December 2011

concept of string class in java for Selenium Automation 2


The concept of Strings in Java is very different from that in C or C++. In Java, a string literal is an object of type String class. Hence manipulation of strings will be done through the use of the methods provided by the String class. When a string object is being created one cannot change the characters that make up a string. But how can one still change the string? Every time an altered version of the string is required, a new string object is created with the modifications in it. The original string remains unchanged.
Constructors of the String class
The following table shows the constructors of the string class
JAVA: String class 
String length
The length() method provided by String class is used for determining the length of a string. It returns the number of characters in the string.
Its syntax is:
public int length()
The following code fragment prints 5 since there are 5 characters in the string s
char ch[] = {‘a’, p’, p’, l’, e’};
String s = new String(ch);
System.out.println(s.length());
String comparison
The == operator and equals() method can be used for string comparison, however their results may not exactly be the same. The == operator checks if two operands being used are one and the same object whereas the equals() method checks if the character forming the contents of the two operands are the same. The following example shows the use of both the operators:
class CheckEquality
{
    
public static void main(String args[])
    
{
        
String str1 = new String("Apple");
        
String str2 = new String(str1);
        
System.out.println("str1 equals str2 : "+ str1.equals(str2));
        
System.out.println("str1 == str2 : " +(str1 == str2));
    
}
}
The output of the above code is:
JAVA: String class
The second println prints false because str1 and str2 do not refer to the same object. They are two different objects.
Searching Strings
The String class also provides a variety of methods to perform search operations. The indexOf() method searches within a string for a given character or String. It returns the location where the first match occured or returns a –1 if the character or string was not found. Following are some of the overloaded methods of indexOf():
• public int indexOf(int ch): Searches for the first occurrence of character ch.
• public int indexOf(String value): Searches for the first occurrence of the string value.
• public int lastIndexof(int ch): Searches for the last occurrence of character ch.
• public int lastIndexof(String value): Searches for the last occurrence of the string value.
Changing the case of characters within a String
The method toLowerCase() and toUpperCase() will convert all characters in a string either to lower case or upper case. Both the methods return a string object. The syntax of the functions is:
• public String toUppercase()
• public String toLowercase()
The following example shows using these methods:
class StringTest
{
    
public static void main(String args[])
    
{
        
String name = args[0];
        
if (name.startsWith("G"))
        
System.out.println("Hey my name also starts with a G");

        
int length = name.length();
        
System.out.println("Your name has " +length+ " characters");
        
String name_in_caps = name.toUpperCase();
        
System.out.println(name_in_caps);
    
}
}
The output generated is:
JAVA: String class