40+ String Class Methods in Java

In this chapter, we are going to learn Java String Class Methods. In our previous chapter, we learned about Java String and String vs StringBuffer vs String Builder.

Therefore, its time to perform some operations on Java String Class. For that, Java provided some inbuild set of methods.

Java String Class Methods

You can use 40+ built-in methods in Java String Class.

charAt()Returns the character at the specified index (position)
codePointAt()Returns the Unicode of the character at the specified index
codePointBefore()Returns the Unicode of the character before the specified index
codePointCount()Returns the Unicode in the specified text range of this String
compareTo()Compares two strings lexicographically
compareToIgnoreCase()Compares two strings lexicographically, ignoring case differences
concat()Appends a string to the end of another string
contains()Checks whether a string contains a sequence of characters
contentEquals()Checks whether a string contains the exact same sequence of characters
copyValueOf()Returns a String that represents the characters of the character array
endsWith()Checks whether a string ends with the specified character(s)
equals()Compares two strings. Returns true if the strings are equal, and false if not
equalsIgnoreCase()Compares two strings, ignoring case considerations
format()Returns a formatted string using the specified locale, format string, and arguments
getBytes()Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array
getChars()Copies characters from a string to an array of chars
hashCode()Returns the hash code of a string
indexOf()Returns the position of the first found occurrence of specified characters in a string
intern()Returns the index within this string of the first occurrence of the specified character
isEmpty()Checks whether a string is empty or not
lastIndexOf()Returns the position of the last found occurrence of specified characters in a string
length()Returns the length of a specified string
matches()Returns the match of string against a regular expression
offsetByCodePoints()Returns the index within this String that is offset from the given index by codePointOffset code points
regionMatches()Tests if two string regions are equal
replace()Searches a string for a specified value, and returns a new string where the specified values are replaced
replaceFirst()Replaces the first occurrence of a substring that matches the given regular expression
replaceAll()Replaces all occurrences of the string that matches the given regular expression
split()Splits a string into an array of substrings
startsWith()Checks whether a string starts with specified characters
subSequence()Returns a new character sequence that is a subsequence of this sequence
substring()Extracts the characters from specified position, and returns the specified number of character
toCharArray()Converts this string to a new character array
toLowerCase()Converts a string to lower case letters
toString()Returns the value of a String object
toUpperCase()Converts a string to upper case letters
trim()Removes whitespace from both ends of a string
valueOf()Returns the primitive value of a String object

How to Use the Java String Method:

Let’s take an example of some Java String Class methods.

public class Practise {

	public static void main(String[] args) {

		String myString = "Hello World";

		// charAt() Example
		char value1 = myString.charAt(2);
		// prints :: l

		// codePointAt() Example
		int value2 = myString.codePointAt(3);
		// prints :: unicode of char at index 3 i.e 108

		// length() Example
		int value3 = myString.length();
		// prints :: length of string i.e 11

Recommended Reads: