Search⌘ K
AI Features

Basic String Operations

Explore the fundamental string operations used in Java programming, including slicing, concatenation, repetition, splitting, and joining. Understand how these operations work internally, their impact on performance, and how to apply them effectively in string algorithms and problem solving.

Now that we’ve established that strings are immutable and that any apparent modification produces a new string rather than altering the original, the next step is to build familiarity with the operations that are actually available on strings. These operations form the practical foundation of string algorithm implementation. Each one has a defined behavior and a measurable cost, and understanding both is essential before moving on to writing algorithms that use them.

Slicing: Extracting part of a string

Slicing is the operation of extracting a contiguous portion of a string. The extracted portion is called a substring, and it is returned as a new string. The original string remains unchanged.

In Java, substring extraction uses the following syntax:

s.substring(start, end)

The result contains all characters from the start index up to, but not including, the end index. Consider the following example:

Java 25
class Main {
public static void main(String[] args) {
// Initialization of the string data
String s = "hello world";
// Accessing the first five characters (indices 0 to 4)
// The character at index 5 (' ') is excluded.
System.out.println("Substring from index 0 to 5: \"" + s.substring(0, 5) + "\"\n");
// Accessing characters from index 6 to the end (indices 6 to 11)
// This represents the second word in the sequence.
System.out.println("Substring from index 6 to 11: \"" + s.substring(6, 11) + "\"\n");
// Accessing a shorter prefix (indices 0 to 2)
System.out.println("Substring from index 0 to 3: \"" + s.substring(0, 3) + "\"");
}
}

In this example, s.substring(0, 5) extracts the first five characters, and s.substring(6, 11) extracts the last five. The character at the end index is never included in the result.

Now, let’s look at the following visualizer for a better understanding of slicing in strings:

Omitting start and end in slicing

In Java, default boundaries are handled explicitly:

  • To slice from the beginning, use s.substring(0, end).

  • To slice to the end, use s.substring(start).

  • To copy the entire string, use s.substring(0) or simply new String(s).

Java 25
class Main {
public static void main(String[] args) {
String s = "hello world";
// Accessing from the start of the string to index 4 (indices 0 to 4)
// In Java, explicitly pass 0 as the start index.
System.out.println("Substring from the start to index 5: \"" + s.substring(0, 5) + "\"\n");
// Accessing from index 6 to the end of the string
// When only start is provided, substring extends to the end.
System.out.println("Substring from index 6 to the end: \"" + s.substring(6) + "\"\n");
// Accessing the entire string sequence
// Using substring(0) creates a complete copy of the string.
System.out.println("Full string copy using substring: \"" + s.substring(0) + "\"");
}
}

Step in slicing

In Python, slicing accepts a third step argument. Java does not have built-in step slicing, but the same effect can be achieved with a loop or StringBuilder:

// Step equivalent in Java
StringBuilder result = new StringBuilder();
for (int i = start; i < end; i += step) {
result.append(s.charAt(i));
}

A step of two selects every second character, and a step of -1 traverses the string in reverse order.

Java 25
class Main {
public static void main(String[] args) {
String s = "hello world";
// The step determines the increment between indices.
// Indices accessed: 0, 2, 4, 6, 8, 10
StringBuilder everySecond = new StringBuilder();
for (int i = 0; i < s.length(); i += 2) {
everySecond.append(s.charAt(i));
}
System.out.println("Every second character from the string: \"" + everySecond + "\"\n");
// Accessing the string in reverse order
// A negative step of -1 starts from the end and moves toward the beginning.
String reversed = new StringBuilder(s).reverse().toString();
System.out.println("The string in reverse order: \"" + reversed + "\"");
}
}

Complexity analysis of slicing

Producing a slice of length kk requires allocating a new string and copying kk characters into it. Substring therefore takes O(k)O(k) ...