Java String Methods

Built-in methods for manipulating text in Java

📝 What are String Methods?

String methods are built-in functions that help you manipulate text in Java. They let you change, search, and format strings easily without writing complex code yourself.


// Basic string methods example
String text = "Hello World";
System.out.println(text.length());      // 11
System.out.println(text.toUpperCase()); // HELLO WORLD
System.out.println(text.charAt(0));     // H
                                    

Types of String Methods

📏

Length & Size

Get information about string size

length() isEmpty() isBlank()
🔍

Search & Find

Find characters and substrings

indexOf() contains() startsWith()
✂️

Cut & Extract

Get parts of strings

substring() charAt() split()
🔄

Transform

Change string format and case

toUpperCase() toLowerCase() replace()

🔹 Basic String Information

Get basic information about your strings:

public class StringInfo {
    public static void main(String[] args) {
        String name = "Java Programming";
        
        // Get string length
        System.out.println("Length: " + name.length());
        
        // Check if empty
        String empty = "";
        System.out.println("Is empty: " + empty.isEmpty());
        
        // Get character at position
        System.out.println("First char: " + name.charAt(0));
        System.out.println("Last char: " + name.charAt(name.length() - 1));
    }
}

Output:

Length: 16

Is empty: true

First char: J

Last char: g

🔹 Searching in Strings

Find text within strings:

public class StringSearch {
    public static void main(String[] args) {
        String sentence = "I love Java programming";
        
        // Check if contains text
        System.out.println("Contains 'Java': " + sentence.contains("Java"));
        
        // Find position of text
        System.out.println("Position of 'Java': " + sentence.indexOf("Java"));
        
        // Check start and end
        System.out.println("Starts with 'I': " + sentence.startsWith("I"));
        System.out.println("Ends with 'ing': " + sentence.endsWith("ing"));
    }
}

Output:

Contains 'Java': true

Position of 'Java': 7

Starts with 'I': true

Ends with 'ing': true

🔹 Extracting Parts of Strings

Get specific parts of your strings:

public class StringExtract {
    public static void main(String[] args) {
        String fullName = "John Smith";
        
        // Get substring
        String firstName = fullName.substring(0, 4);
        String lastName = fullName.substring(5);
        
        System.out.println("First name: " + firstName);
        System.out.println("Last name: " + lastName);
        
        // Split string
        String[] parts = fullName.split(" ");
        System.out.println("Parts: " + parts[0] + " and " + parts[1]);
    }
}

Output:

First name: John

Last name: Smith

Parts: John and Smith

🔹 Transforming Strings

Change the format and content of strings:

public class StringTransform {
                    public static void main(String[] args) {
                        String message = "  Hello World  ";
                        
                        // Change case
                        System.out.println("Upper: " + message.toUpperCase());
                        System.out.println("Lower: " + message.toLowerCase());
                        
                        // Remove spaces
                        System.out.println("Trimmed: '" + message.trim() + "'");
                        
                        // Replace text
                        String newMessage = message.replace("World", "Java");
                        System.out.println("Replaced: " + newMessage);
                    }
                }

Output:

Upper: HELLO WORLD

Lower: hello world

Trimmed: 'Hello World'

Replaced: Hello Java

🔹 Practical String Methods

Real-world examples of string manipulation:

Common Use Cases:

  • Email validation: Check if contains "@" symbol
  • Name formatting: Capitalize first letters
  • Data cleaning: Remove extra spaces
  • Text processing: Split sentences into words
public class PracticalExample {
    public static void main(String[] args) {
        // Email validation
        String email = "[email protected]";
        if (email.contains("@") && email.contains(".")) {
            System.out.println("Valid email format");
        }
        
        // Format name
        String name = "john doe";
        String[] words = name.split(" ");
        String formatted = "";
        for (String word : words) {
            formatted += word.substring(0, 1).toUpperCase() + 
                        word.substring(1).toLowerCase() + " ";
        }
        System.out.println("Formatted: " + formatted.trim());
    }
}

🧠 Test Your Knowledge

Which method returns the number of characters in a string?