Java String Functions

The class String includes various Java String methods or functions for examining individual characters in a string. The Java String Methods perform string comparison, search, copy, extracting substring, and converting to lowercase or uppercase.

Java String Functions

The following table shows the list of Java Methods or functions available in String Class

Modifier TypeMethodsDescription
charchartAt(int index)It returns the Character at the specified index position
intcodePointAt(int index)This returns the Unicode of the Character at a specified index position
intcodePointBefore(int index)It returns the Unicode of the Character before the specified index position
intcodePointCount(int start_index, int end_index)It returns the Unicode of the Character at the specified index position
intcomapreTo(String str)It compares two string lexicographical
intcomapreToIgnoreCase(String str)This method compares two strings lexicographically. While comparing, this method ignores the case difference.
Stringconcat(String str)This method helps to Join the user-specified string to the end of the string and return the new string.
booleancontains(CharSequence s)This returns TRUE if and only if the string contains the user-specified sequence of characters.
booleancontentEquals(CharSequence s)It compares the string with the user-specified sequence of characters
booleancontentEquals(StringBuffer sb)It compares the string with the user-specified StringBuffer
Static StringcopyValueOf(char[] anCharArray)This Java String method returns the string that represents the character sequence in the specified array.
Static StringcopyValueOf(char[] anCharArray, int offset, int count)Returns the string that represents the character sequence in the specified array, starting at offset until it reaches the specified count.
booleanendsWith(String suffix)It test whether the string is ending with the suffix that we specified or not
booleanequals(Object anObject)It compares this string with the user-specified object
BooleanequalsIgnoreCase(String another_String)It compares this string with the user-specified string (another_String). Note, while comparing, it ignores the case sensitivity.
static Stringformat (String format, Object.. args)Java string functions return a formatted string using the user-specified format and arguments
static Stringformat (locale l, String format, Object.. args)

It returns a formatted string using the user-specified locale, format, and arguments

byte[]getBytes()It converts the string into a sequence of bytes using the platform default charset and stores it in a byte array.
byte[]getBytes(Charset charset)It converts the given string into a sequence of bytes using the specified charset and stores it in a byte array
byte[]getBytes(String charset_Name)This Java string functions convert the string into a sequence of bytes using the named charset and stores it in a byte array.
inthashCode()It returns the Hash code of the Java string
intindexOf(int ch)It returns the index position of the first occurrence of a specified character. It returns -1 if the specified character is not found
intindexOf(int ch, int Starting_Index)It returns the index position of the first occurrence of a given character. It returns -1 if the specified character is not found
intindexOf(String str)It returns the index position of the first occurrence of a specified substring. It returns -1 if the specified string is not found
intindexOf(String str, int Start_Index)It returns the index position of the first occurrence of a specified substring. It returns -1 if the specified string is not found
Stringintern()This function returns the canonical representation for the String Object
booleanisEmpty()This function returns TRUE if the length of this string is Zero
intlastIndexOf(int ch)It returns the index position of the last occurrence of a specified character. It returns -1 if the given character is not found
intlastIndexOf(int ch, int End_Index)Returns the index position of the last occurrence of a specified character. It returns -1 if the specified character is not found
intlastIndexOf(String str)It returns the index position of the last occurrence of a given character. It returns -1 if the specified character is not found
intlastIndexOf(String str, int End_Index)Returns the index position of the last occurrence of a specified character. It returns -1 if the given character is not found
intlength()This returns the length of this string
booleanmatches(String regexp)This function tells whether this string matches a user-specified regular expression or not.
intoffsetByCodePoints(int index, int codePointOffset)It returns the index within the string that is offset of a given index by codePointOffset code points
booleanregionMatches(int toffset, String other, int offset, int len)This Java String Methods test if two string regions are equal or not
booleanregionMatches(boolean ignorecase, int toffset, String other, int offset, int len)It test if two string regions are equal or not
Stringreplace(char Old_Character, char New_Character)This Java string functions search for specified character and then replace all the occurrences of Old_Character with new New_Character
Stringreplace(CharSequence Old_sequence, CharSequence New_sequence)It searches for a specified substring and then replaces all the occurrences of the Old_sequence substring with the new New_sequence substring.
StringreplaceAll(String regexp, String replacement)This Method searches for a specified regular expression. Then replace all the occurrences of the regular expression substring with the given replacement substring.
StringreplaceFirst(String regexp, String replacement)

It searches for a specified regular expression and then replaces the first occurrence of the regular expression substring with the given replacement substring.

String[]split(String regex)It Split the string into an Array of Substrings based on the separator we specified
String[]split(String regex, int limit)This one Split the string into an Array of Substrings based on the separator we specified
booleanstartsWith(String prefix)It tests whether the string is starting with the prefix that we specified or not
booleanstartsWith(String prefix, int limit)It tests whether the substring of this string is starting with the prefix that we specified or not
CharSequencesubsequent(int Starting_Index, int End_Index)This Java String Method returns a new sequence that is after the given sequence.
Stringsubstring(int Starting_Index)It extracts the characters from a string based on the specified indices
Stringsubstring(int Starting_Index, int End_Index)It extracts the characters from a string based on the specified starting position and end position.
char[]toCharArray()This converts the given string into an Array of Characters or Character Array
inttoLowerCase()It converts the given string into Lowercase letters using the default locale
StringtoLowerCase (Locale locale)It converts the given string into Lowercase letters by considering the user-specified locale
stringtoString()This Java String Method converts the given value into a String Object
string toUpperCase()It converts the given string into Uppercase letters
stringtoUpperCase (Locale locale)It converts the given string into Uppercase letters by considering the host environment’s current locale
stringtrim()This removes the white spaces from both ends
static StringValueOf(boolean number)Returns the string representation of Boolean value
ValueOf(char character)String representation of char
ValueOf(char[] myArray)String representation of a character array
ValueOf(char[] myArray, int Offset, int count)This returns the string representation of a subset character array.
ValueOf(double number)String representation of Double value
ValueOf(float number)String representation of Float value
ValueOf(int number)It returns the string representation of the Integer value.
ValueOf(long number)It returns the string representation of the Long value.
ValueOf(Object obj)String representation of an Object.