Method of String Class

123 views.

1) int length();

This method will return the count of the number of characters available in a string.

Sample Program

Output:

12

2) char charAt(int index):

This method will return a character that is available at the specified index position.

The index position starts from 0. If the specified index is not within the range then, it will generate a runtime error called StringIndexOutOfBoundsException.

Example:

3)String concat(string):

This method can be used to append the contents of one string to another string.

Example:

4) int comapreTo(String):

This method can be used to compare the unicode values of the characrters available in a String, by considering their case. This method is designed for sorting the strings.

s1>s2   +ve

sa<s2   -ve

s1==s2   0

5)int compareToIgnoreCase(String):

This method can be used to compare the unicode values of the characters available in a string by ignoring their case. This method is designed for sorting the strings.

Example

6) boolean equal(Object):

This method can be used to compare the contents of the string objects by considering their case.

7) boolean equalIgnoreCase(String):

This method can be used to compare the content of the string objects by ignoring their case.

Exanmple:

8) boolean startsWith(String):

This method can be used to check whether a string begins with a specified group of characters or not.

9) boolean endsWith(String):

This method can be used to check whether a string ends with a specified group of characters or not.

Note: The startsWith() and endsWith() will consider the case.

Example:

10)int indexOf(char):

This method will return the index of the first occurrence of the specified character.

11)int lastIndexOf:

This method will return the index of the last occurrence of the specified character.

Note:

The indexOf() and lastIndexOf() will return -1, if the specified character is not available

Example:

12) String replace(char old, char new):

This method can be used to replace all the occurrence of the specified character with a new character.

Example:

13)String substring(int index):

This method will return a group of characters beginning from the specified index position up to the end of the string.

14)String substring(int index, int offset):

The method will return a group of characters  beginning from the specified index up to specified offset.

Note: The offset represents the position of a character in a string and it begins with 1.

Example:

15) String toLower():

This method will convert the contents of a string to completely lower case.

16) String toUpper():

This method will convert the contents of a string to completely upper case.

Example:

17) String trim():

This method can be used to remove the leading and trailing spaces available in a string.

Example:

18) String intern():

This method will refer to an object avaible in string constant pool which was created at the time creation in heap memory.

Example:

 

 

 

 

 

Leave a Reply