×

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

How to check valid date in Java?

Every time we get data for any application, we must first ensure that it is accurate before continuing with any further processing. We might have to confirm the following while dealing...

4 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Java vs Dot Net

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

7 minutes read.

Selection Sort in Java

Selection Sort in Java Selection sort is also a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted portion of the input array, and placing it...

5 minutes read.

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

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

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Bad Operand types for Binary Operator Java

We will discuss how to handle issues in bad operand types for binary operator &, bad operand types for binary operator &&, bad operand types for binary operator ==, and...

4 minutes read.

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.

ArrayList vs Vector in Java

ArrayList Vs. Vector in Java In Java, the two classes ArrayList and Vector both are associated with Java Collections Framework. Both classes implement java.util.List interface. Even so, these classes have noticeable...

4 minutes read.

Java Boolean Class

The Boolean class wraps a value of the Boolean primitive in an object. Its object contains only a single field whose type is Boolean. Boolean Methods: This class contains several different methods...

2 minutes read.

Flag Pattern in Java

The flag pattern in Java can be printed, which will be covered in this part. Given how difficult they are to code, flag patterns are rarely asked by interviewers. We separate...

2 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

How to Calculate Time Difference Between Two Dates in Java?

Date is being used extensively in Java to calculate date differences. While constructing an application, the date of joining an organisation, admission date, appointment date, and others might be included....

4 minutes read.