Java String class methods

The java.lang.String class provides a lot of methods to effort on string. By the help of these methods, we can implement processes on string such as trimming, concatenating, converting, comparing, replacing strings etc.

Here is the list of methods supported by String class

Method Description
1. public boolean equals(Object anObject) Compares this string to the specified object.
2. public boolean equalsIgnoreCase(String another) Compares this String to another String, ignoring case.
3. public String concat(String str) Concatenates the specified string to the end of this string.
4. public int compareTo(String str) Compares two strings and returns int
5. public int compareToIgnoreCase(String str) Compares two strings, ignoring case differences.
6. public String substring(int beginIndex) Returns a new string that is a substring of this string.
7. public String substring(int beginIndex,int endIndex) Returns a new string that is a substring of this string.
8. public String toUpperCase() Converts all of the characters in this String to upper case
9. public String toLowerCase() Converts all of the characters in this String to lower case.
10. public String trim() Returns a copy of the string, with leading and trailing whitespace omitted.
11. public boolean startsWith(String prefix) Tests if this string starts with the specified prefix.
12. public boolean endsWith(String suffix) Tests if this string ends with the specified suffix.
13. public char charAt(int index) Returns the char value at the specified index.
14. public int length() Returns the length of this string.
15. public String intern() Returns a canonical representation for the string object.
16. public byte[] getBytes() Converts string into byte array.
17. public char[] toCharArray() Converts string into char array.
18. public static String valueOf(int i) converts the int into String.
19. public static String valueOf(long i) converts the long into String.
20. public static String valueOf(float i) converts the float into String.
21. public static String valueOf(double i) converts the double into String.
22. public static String valueOf(boolean i) converts the boolean into String.
23. public static String valueOf(char i) converts the char into String.
24. public static String valueOf(char[] i) converts the char array into String.
25. public static String valueOf(Object obj) converts the Object into String.
26. public void replaceAll(String firstString,String secondString) Changes the firstString with secondString.

Java String toUpperCase() and toLowerCase() method

The java string toUpperCase() method converts this string into uppercase letter and string toLowerCase() method into lowercase letter.


String s="Mithilesh";  
System.out.println(s.toUpperCase());//MITHILESH 
System.out.println(s.toLowerCase());//Mithilesh  
System.out.println(s);//Mithilesh(no change in original)