String Methods in Java


One of the most vital parts of Java is Strings. As a Java programmer, one of your main tools for storing and processing data is the String class.
What makes Strings so powerful in Java? The answer to that will be methods of String class. The java.lang.String provides a bunch of useful methods which can be used to perform various operations on a sequence of character values.
Let us list out all the methods which falls under the String class. You can just click on any method and read about it in detail with explained example codes.


String Methods

charAt(int index)
– Returns the character value at the specified index. The index value should be between 0 to length()-1, both inclusive.
codePointAt(int index)
– Returns the Unicode value of the character at the specified index.
codePointBefore(int index)
– Returns the Unicode value of the character before the specified index.
codePointCount(int beginIndex, int endIndex)
– Returns the number of Unicode point values in the specified range of the String.
compareTo(String anotherString)
– Compares two strings in dictionary order, also known as lexicographically.
concat(String anotherString)
– Concatenates the specified string to the end of this string.
conatins(CharSequence cs)
– Returns true or false after checking if the specified sequence of characters is present in the string or not.
contentEquals(CharSequence cs)
– Compares this string to the specified character sequence.
copyValueOf(char[ ] data)
– Returns a string that represents the character sequence in the array specified.
endsWith(String suffix)
– Checks whether the string ends with the specified characters or not.
equals(Object anobject)
– Returns true or false after comparing this string to the specified string.
indexOf(int ch)
– Returns the index within the string of the first occurrence of the specified character/substring.
length()
– Returns the length of the specified string.
lastIndexOf(int ch)
- Returns the index within the string of the last occurrence of the specified character/substring.
replace(char oldChar, char newChar)
– Returns a new string by replacing all the occurrences of oldChar with newChar.
startsWith(String prefix)
– Returns true of false after checking if the string starts with the specified prefix.
substring(int beginIndex)
– Returns a new string that is a substring of this string.
toLowerCase() and toUpperCase()
– Converts all the characters of the string to lower case and upper case respectively.

Summary

We have seen the overview of String methods in Java. Make sure to check each one of these methods in detail and get a fair idea about how it works. Some of the methods listed above have sub-categories depending upon the parameters specified.

Read More

Author : Satyam Kumar -



You will also like: