Practice Strings in Java

String manipulation, pattern matching, and text processing in Java.

Work through 19 exercises with starter code, expected output, test cases, and hints.

How to learn Strings

A Java String is an immutable sequence of UTF-16 code units. Operations such as toUpperCase, replace, and substring return a value; they do not edit the original object. Immutability makes strings safe to share, but repeated concatenation in a loop can allocate many temporary objects.

Decide whether a task works with char values, Unicode code points, or whole words. A char is sufficient for many introductory ASCII exercises, but it cannot represent every Unicode character by itself. That distinction matters in production text processing even when a practice input is simple.

Recommended learning order

  1. Inspect without changing: Practice length, charAt, indexOf, contains, startsWith, and substring while checking bounds.
  2. Compare content: Use equals or equalsIgnoreCase for content and understand why == only answers whether two references identify the same object.
  3. Build efficiently: Use StringBuilder when appending or editing repeatedly, then convert to String at the boundary.
  4. Recognize text patterns: Move to frequency counts, two pointers, sliding windows, and normalization only after basic methods are predictable.

Tested example: Normalize and test a palindrome

The method builds a lowercase alphanumeric representation once, then compares mirrored characters. The definition is explicit: punctuation and letter case do not affect the result.

public class Main {
    static boolean isPalindrome(String text) {
        StringBuilder clean = new StringBuilder();
        for (char ch : text.toCharArray()) {
            if (Character.isLetterOrDigit(ch)) {
                clean.append(Character.toLowerCase(ch));
            }
        }
        for (int left = 0, right = clean.length() - 1; left < right; left++, right--) {
            if (clean.charAt(left) != clean.charAt(right)) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isPalindrome("Never odd or even"));
    }
}

Expected output

true

Change and run this example in the Java compiler

Common mistakes

  • Comparing text with == instead of equals, producing results that depend on object identity and string interning.
  • Ignoring the returned value of trim, replace, or toUpperCase because Strings cannot be modified in place.
  • Appending with + inside a large loop when one StringBuilder would avoid repeated copying.
  • Calling a method on a possibly null reference instead of defining whether null is invalid or has a special meaning.

How to practice

Start with inspection and transformation methods, then reverse and palindrome tasks, and finally frequency maps and window problems. For every comparison exercise, test empty text, one character, mixed case, and repeated characters.

Exercises to do first

Strings exercises

  1. String Length Java exercise — Find the length of a string
  2. Convert to Uppercase Java exercise — Convert a string to uppercase
  3. Character at Index Java exercise — Get character at a specific position
  4. Extract Substring Java exercise — Extract a portion of a string
  5. Check Contains Java exercise — Check if a string contains a substring
  6. Replace Characters Java exercise — Replace parts of a string
  7. Reverse String Java exercise — Reverse a string
  8. String Palindrome Java exercise — Check if a string is a palindrome
  9. Count Vowels Java exercise — Count vowels in a string
  10. Count Words Java exercise — Count words in a sentence
  11. First Non-Repeating Character Java exercise — Find the first character that does not repeat
  12. Anagram Check Java exercise — Check if two strings are anagrams
  13. Longest Word Java exercise — Find the longest word in a sentence
  14. Remove All Spaces Java exercise — Remove all whitespace from a string
  15. Character Frequency Java exercise — Count frequency of each character
  16. String Compression Java exercise — Compress string using counts of repeated characters
  17. Alphabetical Character Frequency Java exercise — Count frequency of each character
  18. Remove Adjacent Duplicates Java exercise — Remove adjacent duplicate characters
  19. Zigzag Conversion Java exercise — Convert string to zigzag pattern

Browse the complete Java practice path · Open the online Java compiler