×

Java String Methods

Java String Methods

Java String class is the most important class of the java.lang package. It is used to handle the String related operations. It contains a lot of built-in Java string methods that make our task easier.  The following table describes the String class methods.

MethodReturn TypeDescription
length()intReturns the total number of characters present in the string.
charAt()charAt the given index, whatever the character is present gets returned.
concat()StringAdds a string at the end of another string
equals()booleanCompares two string and returns true when there is a match otherwise return false. The equals() method considers the case - sensitivity of characters present in the strings.
equalsIgnoreCase()booleanCompares two string and returns true when there is a match otherwise, returns false. The equalsIgnoreCase() method does not consider the case - sensitivity of characters present in the strings.
contains()booleanIn a given string, the method looks for a specified sequence of characters. If found true is returned; otherwise, false.
isEmpty()booleanChecks whether a given string is empty or not. If the given string has no characters, i.e., found empty, true is returned; otherwise, false.
lastIndexOf()intReturns the index of the last occurrence of the characters specified in the given string.
hashCode()intHash code of the given string is returned by this method.
endsWith()BooleanChecks whether the given string ends with the specified characters or not
split()String[]The split() method breaks the given string into the array of smaller substrings.
toLowerCase()StringConvert each and every character of the given into small case.
toUpperCase()StringConvert each and every character of the given into upper case.
trim()StringRemoves white spaces from both ends of the given string.
replaceAll()StringReplaces each and every occurrence of the substring that is matching with the given string.
replaceFirst()StringReplaces the first occurrence of the substring that is matching with the given string.
compareTo()intCompares two strings lexicographically. The compareTo() method takes case sensitivity into consideration.
compareToIgnoreCase()intCompares two strings lexicographically. The compareToIgnoreCase () method does not take case sensitivity into consideration.
indexOf()intLooks for the occurrence of the first position of the given character in the specified string.
toCharArray()char[]Converts the specified string into the character array.
substring()StringExtracts characters from the given string from the specified position and returns it.
regionMatches()booleanChecks whether regions of the two given strings match or not.
getChars()voidCreates a copy of characters from a string to the specified array of characters.
getBytes()byte[]Encodes the specified string into a stream of bytes.
matches()booleanLooks for a string that matches the specified regular expression. 
intern()StringReturn the canonical representation of the specified string object.
subsequence()CharSequenceReturn a new character sequence that is sequence of the specified sequence.
codePointBefore()intReturns the Unicode of the character that is present just before the specified character.
codePointAt()intReturns the Unicode of the character that is present at the specified index.
valueOf()StringThe method converts any specified data type into the string and then returns it.

Related Topics

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.

Java String contains() method

contains() method returns true only if it contains the given sequence of characters otherwise it returns false. syntax: public boolean contains(CharSequence sequence) parameters: sequence : It is the sequence to be searched. Returns: It returns true...

1 minute read.

Convert Char array to string in java

A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string...

4 minutes read.

Java Math cbrt() Method

The cbrt() method of Math class returns the cube root of a double value. Syntax: public static double cbrt(double a) Parameters: The parameter ‘a’ represents the value whose cube root is to be determined. Return...

2 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns...

2 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

Java String replaceFirst() method

This method replace the first substring with user specified String. Syntax: public String replaceFirst(String Regexp, String Replacement) Parameter: Regexp : Regular Expression Replacement : the String which would replace found expression Return: a string Java String replaceFirst()...

1 minute read.

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

Comparetoignorecase in Java

In Java, the function compareToIgnoreCase ( ) is part of the String class, which is part of the java.lang package. It is used to compare any two strings while disregarding...

4 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

How to sort an array in Java

Sorting is the process of arranging the elements of a list or array in a specific order, either ascending or descending. The sorting criterion numerical and alphabetical is commonly used...

6 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.