×

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

&& Operator in Java

“ && ” is the conditional - And operator in Java. In Java, it is an example of a logical operator. In Java, the “ & ” operator has two...

3 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

Java Math rint() Method

The rint() method of Java Math class returns the double value which is close to the specified argument and is equal to mathematical integer. Syntax: public static double rint(double a) Parameters: The parameter ‘a’...

1 minute read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Magic Square in Java

A square with numbers is referred to as a magic square. The numbers in a magic square of order n are arranged to ensure that the sum of all of...

4 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

Java Math decrementExact() Method

The decrementExact() method of Math class returns the argument which is decremented by one, throwing an exception is the result overflows an int or long. Syntax: public static int decrementExact (int a) Parameters: The...

1 minute read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.

Producer consumer problem in Java using Synchronised block

The producer-consumer problem in Java, commonly known as the bounded-buffer problem, is a well-known multi-process synchronization challenge where we attempt to synchronize many processes. Two processes are involved in the producer-consumer problem:...

3 minutes read.

Java Math log10() Method

The log10() method of Math class returns the base 10 logarithmic value for the specified double argument. Syntax: public static double log10(double a) Parameters: The parameter ‘a’ represents a double value. Return Value: The log10() method...

2 minutes read.

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.

Armstrong Number Program in Java

Armstrong Number Program in Java: A positive number is called an Armstrong number if the sum of the cube of each digit is equal to the number itself. There are...

6 minutes read.

Java OCR

In this article, you will be acknowledged about what is a tesseract OCR, how it works, what are its used and advantages and disadvantages. Also, you will be able to...

4 minutes read.

Java Switch string

A multi-way branch statement is the switch statement. It offers a simple method for allocating execution to various code sections according to the expression's value. Primitive data types, including bytes,...

3 minutes read.

How to create array of objects in Java

Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates...

4 minutes read.