IMPORTANT METHODS IN String CLASS:

■ charAt() Returns the character located at the specified index
■ concat() Appends one String to the end of another ( "+" also works)
■ equalsIgnoreCase() Determines the equality of two Strings, ignoring case
■ length() Returns the number of characters in a String
■ replace() Replaces occurrences of a character with a new character
■ substring() Returns a part of a String
■ toLowerCase() Returns a String with uppercase characters converted
■ toString() Returns the value of a String
■ toUpperCase() Returns a String with lowercase characters converted
■ trim() Removes whitespace from the ends of a String

public char charAt(int index)-This method returns the character located at the String's specified index. Remember, String indexes are zero-based.
for example,
String x = "airplane";
System.out.println( x.charAt(2) ); // output is 'r'

public String concat(String s)-This method returns a String with the value of the String passed in to the method appended to the end of the String used to invoke the method.
for example,
String x = "taxi";
System.out.println( x.concat(" cab") ); // output is "taxi cab"

public String concat(String s)-This method returns a String with the value of the String passed in to the method appended to the end of the String used to invoke the method.
for example,
String x = "taxi";
System.out.println( x.concat(" cab") ); // output is "taxi cab"

public int length()-This method returns the length of the String used to invoke the method.
for example,
String x = "01234567";
System.out.println( x.length() ); // returns "8"

public String replace(char old, char new) -This method returns a String whose value is that of the String used to invoke the method, updated so that any occurrence of the char in the first argument is replaced by the char in the second argument.
for example,
String x = "oxoxoxox";
System.out.println( x.replace('x', 'X') ); // output is
// "oXoXoXoX"

public String substring(int begin)
public String substring(int begin, int end) The substring() method is used to return a part (or substring) of the String used to invoke the method. The first argument represents the starting location (zero-based) of the substring. If the call has only one argument, the substring returned will include the characters to the end of the original String. If the call has two arguments, the substring returned will end with the character located in the nth position of the original String where n is the second argument. Unfortunately, the ending argument is not zero-based, so if the second argument is 7, the last character in the returned String will be in the original String's 7 position, which is index 6 (ouch). Let's look at some examples:
String x = "0123456789"; // as if by magic, the value
// of each char
// is the same as its index!
System.out.println( x.substring(5) ); // output is "56789"
System.out.println( x.substring(5, 8)); // output is "567"

public String toLowerCase()-This method returns a String whose value is the String used to invoke the method, but with any uppercase characters converted to lowercase.
for example,
String x = "A New Moon";
System.out.println( x.toLowerCase() ); // output is
// "a new moon"

public String toString()-This method returns the value of the String used to invoke the method. What? Why would you need such a seemingly "do nothing" method? All objects in Java must have a toString() method, which typically returns a String that in some meaningful way describes the object in question. In the case of a String object, what more meaningful way than the String's value? For the sake of consistency, here's an example:
String x = "hi";
System.out.println( x.toString() ); // output –hi

public String toUpperCase()-This method returns a String whose value is the String used to invoke the method, but with any lowercase characters converted to uppercase.
for example,
String x = "A New Moon";
System.out.println( x.toUpperCase() ); // output is
// "A NEW MOON"

public String trim()-This method returns a String whose value is the String used to invoke the method, but with any leading or trailing blank spaces removed.
for example,
String x = " hi ";
System.out.println( x + "x" ); // result is
// " hi x"
System.out.println( x.trim() + "x"); // result is "hix"

No comments:

Post a Comment