Java String Manipulation Examples

Explore practical examples of Java string manipulation to enhance your coding skills.
By Jamie

Introduction to Java String Manipulation

Java provides a rich set of methods for manipulating strings, making it easier to work with text data. String manipulation is essential in various programming scenarios, such as formatting user input, parsing data, or generating output. In this article, we will explore three diverse examples of Java String manipulation to illustrate different techniques and their use cases.

Example 1: Reversing a String

Use Case

Reversing a string can be useful in applications such as palindrome checking or simply displaying text in reverse order.

public class StringManipulation {
    public static String reverseString(String input) {
        StringBuilder reversed = new StringBuilder(input);
        return reversed.reverse().toString();
    }

    public static void main(String[] args) {
        String original = "Hello, World!";
        String reversed = reverseString(original);
        System.out.println("Original: " + original);
        System.out.println("Reversed: " + reversed);
    }
}

Notes

  • The StringBuilder class is used here for efficient string manipulation.
  • This method can handle strings of any length.

Example 2: Counting Vowels in a String

Use Case

Counting the number of vowels in a string can be helpful in text analytics or while implementing features like text summarization.

public class StringManipulation {
    public static int countVowels(String input) {
        int count = 0;
        String vowels = "AEIOUaeiou";
        for (char c : input.toCharArray()) {
            if (vowels.indexOf(c) != -1) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        String text = "Hello, World!";
        int vowelCount = countVowels(text);
        System.out.println("Number of vowels: " + vowelCount);
    }
}

Notes

  • This example iterates through each character in the input string to count vowels.
  • The indexOf method checks for the presence of each character in the vowels string.

Example 3: String Tokenization

Use Case

Tokenization is useful for breaking down a string into manageable parts, such as parsing command-line arguments or processing CSV data.

import java.util.StringTokenizer;

public class StringManipulation {
    public static void main(String[] args) {
        String data = "Java,Python,C++,JavaScript";
        StringTokenizer tokenizer = new StringTokenizer(data, ",");

        System.out.println("Languages:");
        while (tokenizer.hasMoreTokens()) {
            System.out.println(tokenizer.nextToken());
        }
    }
}

Notes

  • The StringTokenizer class is used to split the string into tokens based on a specified delimiter (in this case, a comma).
  • This method is simple and effective for tokenization but consider using String.split() for more complex scenarios.

By mastering these examples of Java String manipulation, you can enhance your programming skills and tackle a variety of text processing tasks effectively.