×

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

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

Java vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.

Uses of Java

Java is used in many real-world Java applications, including technologies and tools. This Java programming language has become the backbone for developing many applications. In areas like embedded systems and...

3 minutes read.

Java 9 Interface Private Method

Java 9 provides us the facility to include private methods inside the java interface. In java 8 and earlier versions, we are supposed to use only constant variables and abstract...

3 minutes read.

Object Definition in Java

In this article, you will be acknowledged about object in Java, its definition and how is this useful in writing the programs in Java. The comprehension of object-oriented technology depends on...

4 minutes read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Basic Terms in Multithreading

To understand the terms of multithreading, we must have knowledge about concurrency, processes, and threads. Concurrency The concurrency stands for performing multiple tasks at the same time. In the process communication, the operating system permits the process...

8 minutes read.

Tree Implementation in Java

Introduction to Tree A non-linear, hierarchical data structure called a "tree" is made up of a number of nodes, each of which contains a values and a sequence of pointers to...

14 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

Java Math toRadians() Method

The toRadians() method of Java Math class converts an angle measured in degrees to an approximately equivalent angle measured in radian. Syntax: public static double toRadians (double angdeg) Parameters The parameter ‘angdeg’ represents an...

2 minutes read.

Java String Matches vs Contains

String Matches in Java The matches() function and its variations are used to determine whether or not a provided text matches a regular expression. The functioning as well as output of...

3 minutes read.

Java StringReader Class

StreamReader Class technique enables character reading from a string. The java.io package contains this class. A string serves as a source in the character stream. While the Stream class is...

4 minutes read.

Difference Between Java and PHP

The two most used programming languages are PHP and Java. Both of them have a lot of similarities and distinctions. Let's first grasp each of them individually before examining their...

3 minutes read.

Pancake Sorting in Java

In the pancake sorting method, the array must be sorted using just one operation, which is: Flip the arrays arr at index 0 to index j by using flipArr(arr, h). Other sorting...

3 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.

Java Math exp() Method

The exp() method of Math class returns Euler’s number(e) raised to the power of a double value. Syntax: public static double exp(double a) Parameters: The parameter ‘a’ represents the exponent e. Return Value: The exp ()...

2 minutes read.

Catalan number in Java

In general mathematics, Catalan numbers can be defined as the sequence of natural numbers that frequently occur in counting problems often encountered in recursively defined objects. Mathematical formula of Catalan number Coming...

3 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.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.