Search⌘ K
AI Features

Other Common String Operations

Learn how to perform fundamental and additional string operations including searching for substrings, comparing strings, converting case, and trimming spaces. This lesson helps you understand these core techniques and their complexity, preparing you to solve diverse string-based problems in Java efficiently.

Knowing the basic string operations, such as finding the length, traversal, indexing, concatenation, substring, and splitting, is an important start. Knowing additional string operations will better prepare you for a wider range of string algorithms. A problem may ask whether two strings are equal, whether one string starts with another, whether a smaller string appears inside a larger one, or how to form a new string from existing ones.

Searching

Searching refers to the operation of locating a character or a substring within a larger string. At a fundamental level, this is accomplished by scanning through the string and examining each position until the target is found or the end of the string is reached.

Checking for existence

The simplest form of search determines whether a substring exists anywhere within the string. In Java, this is done using the contains() method.

Java 25
class Main {
public static void main(String[] args) {
String s = "hello world";
// Evaluating the presence of the substring "world" within the string s
// The contains() method performs a search to determine if the sequence exists.
// A boolean true is returned if the sequence is identified.
System.out.println("Is \"world\" present in the string? " + s.contains("world") + "\n");
// Evaluating the presence of the substring "xyz" within the string s
// A boolean false is returned if the sequence is not identified.
System.out.println("Is \"xyz\" present in the string? " + s.contains("xyz"));
}
}

The contains() method returns true if the substring is found and false otherwise.

Finding the position

When you need the exact position of the first occurrence, Java provides the indexOf() method, which returns the starting index of the substring. If the substring is not present, indexOf() returns -1.

Java 25
class Main {
public static void main(String[] args) {
String s = "hello world";
// Retrieving the starting index of the first occurrence of "world"
// The indexOf method returns the lowest index where the substring begins.
// If the sequence is identified, an integer index (6) is returned.
System.out.println("The starting index of \"world\" in the string: " + s.indexOf("world") + "\n");
// Attempting to retrieve the starting index of the sequence "xyz"
// If the substring is not present within the string, the method returns -1.
System.out.println("The starting index of \"xyz\" (not present) in the string: " + s.indexOf("xyz"));
}
}

Java does not have a direct equivalent to Python's index() that throws an exception. Instead, the standard approach is to check the return value of indexOf() and handle the -1 case explicitly. ...