Tuesday, 3 April 2012

Class - String


String

Constructors

To create an empty String,  call the default constructor.

Syntax:

                               String String();

Ex,   String s = new String();

Here, it will create an instance of String with no characters in it.

The String class provides a variety of constructors to handle this. To create a String initialized by an array of characters, use the constructor shown here:

                             String(char chars[ ])

Here, the constructor has the char array as an argument.
It is possible to specify a subrange of a character array as an initializer using the following constructor:

          String(char chars[ ], int startIndex, int numChars)

Here, startIndex specifies the index at which the subrange begins, and numChars specifies the number of characters to use.

The constructor can have a String object that contains the same character sequence as another String object using following constructor:
                             String(String strObj)

Here, strObj is a String object.

Methods
LIBRARY
String Length : length()

The length of a string is the number of characters that it contains. To obtain this value,  the length( ) method is used to find length of a string.
Syntax:
int length( )

charAt( )

To extract a single character from a String, the charAt( ) method is used.  
                   char charAt(int where)
Here, where is the index of the character that you want to obtain. The value of where must be nonnegative and specify a location within the string. charAt( ) returns the character at the specified location.

getChars( )
To extract more than one character at a time, getChars( ) method is used.

void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart)

Here, sourceStart specifies the index of the beginning of the substring.

sourceEnd specifies an index that is one past the end of the desired substring. Thus, the substring contains the characters from sourceStart through sourceEnd–1.

The array that will receive the characters is specified by target. The index within target at which the substring will be copied is passed in targetStart. Care must be taken to assure that the target array is large enough to hold the number of characters in the specified substring.

equals( ) and equalsIgnoreCase( )
To compare two strings for equality,equals( ) method is used.

boolean equals(Object str)

Here, str is the String object being compared with the invoking String object. It returns true if the strings contain the same characters in the same order, and false otherwise. The comparison is case-sensitive.

To perform a comparison that ignores case differences, equalsIgnoreCase( ) method is used.

When it compares two strings, it considers A-Z to be the same as a-z.

            boolean equalsIgnoreCase(String str)

Here, str is the String object being compared with the invoking String object. It, too, returns true if the strings contain the same characters in the same order, and false otherwise.
THEVALIBRARY
equals( ) Versus ==
It is important to understand that the equals( ) method and the == operator perform two different operations.
the equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance.
Ex,
class EqualsNotEqualTo
{
public static void main(String args[])
{
String s1 = "Hello";
String s2 = new String(s1);
System.out.println(s1 + " equals " + s2 + " -> " +s1.equals(s2));
System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
}
}

output:Hello equals Hello -> true
Hello == Hello -> false

According to above example ,The variable s1 refers to the String instance created by “Hello”. The object referred to by s2 is created with s1 as an initializer. Thus, the contents of the two String objects are identical, but they are distinct objects. This means that s1 and s2 do not refer to the same objects and are, therefore, not ==.


compareTo( )
This function is used to  compare the string

int compareTo(String str)
Here, str is the String being compared with the invoking String. The result of the comparison is returned and is interpreted as follow:

Value                                                          Meaning

Less than zero                The invoking string is less than str.
Greater than zero            The invoking string is greater than str.
Zero                                The two strings are equal

indexOf() lastIndexOf()

To search for the first occurrence of a character, indexOf() is used.

int indexOf(int ch)
To search for the last occurrence of a character, lastIndexOf() is used.

int lastIndexOf(int ch)
Here, ch is the character being sought.

To search for the first or last occurrence of a substring, use
int indexOf(String str)
int lastIndexOf(String str)
Here, str specifies the substring.

To specify a starting point for the search using these forms:

int indexOf(int ch, int startIndex)
int lastIndexOf(int ch, int startIndex)
int indexOf(String str, int startIndex)
int lastIndexOf(String str, int startIndex)

Here, startIndex specifies the index at which point the search begins. For indexOf( ), the search runs from startIndex to the end of the string. For lastIndexOf( ), the search runs from startIndex to zero.
IBRARY
substring( )

To extract a substring substring( ) is used. It has two forms. The first is
            String substring(int startIndex)

Here, startIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at startIndex and runs to the end of the invoking string.

The second form of substring( ) allows you to specify both the beginning and ending index of the substring:

String substring(int startIndex, int endIndex)

Here, startIndex specifies the beginning index, and endIndex specifies the stopping point. The string returned contains all the characters from the beginning index, up to, but not including, the ending index.

concat( )
To concatenate two strings  concat( ) is used.

String concat(String str)

This method creates a new object that contains the invoking string with the contents of str appended to the end.
concat( ) performs the same function as +.

String s1 = "one";
String s2 = s1.concat("two");
puts the string “onetwo” into s2.

It generates the same result as the following sequence:
String s1 = "one";
String s2 = s1 + "two";

replace( )

The replace( ) method replaces all occurrences of one character in the invoking string with another character.

String replace(char original, char replacement)

Here, original specifies the character to be replaced by the character specified by replacement. The resulting string is returned.
Ex, String s = "Hello".replace('l', 'w');
puts the string “Hewwo” into s.

trim( )

The trim( ) method returns a copy of the invoking string from which any leading and trailing whitespace has been removed.

String trim( )
String s = " Hello World ".trim();
This puts the string “Hello World” into s.

toLowerCase() , toUpperCase()

The method toLowerCase( ) converts all the characters in a string from uppercase to lowercase.

The toUpperCase( ) method converts all the characters in a string from lowercase to uppercase.Nonalphabetical characters, such as digits, are unaffected.

String toLowerCase( )
String toUpperCase( )

Both methods return a String object that contains the uppercase or lowercase equivalent of the invoking String.


Posted By : Ruchita Pandya

No comments:

Post a Comment