Bird
Raised Fist0
Javaprogramming~5 mins

Common string methods in Java

Choose your learning style10 modes available
Introduction

String methods help you work with text easily. They let you check, change, or find parts of words or sentences.

When you want to check if a word contains a certain letter or part.
When you need to change all letters to uppercase or lowercase.
When you want to find where a word or letter appears in a sentence.
When you want to cut out a piece of a sentence.
When you want to replace some words with others.
Syntax
Java
string.methodName(arguments);
Replace string with your text variable or text in quotes.
Replace methodName with the name of the string method you want to use.
Examples
Check if the text contains the word "Hello".
Java
String text = "Hello World";
boolean hasHello = text.contains("Hello");
Change all letters in the text to uppercase.
Java
String text = "Hello World";
String upper = text.toUpperCase();
Find the position of the letter 'W' in the text.
Java
String text = "Hello World";
int pos = text.indexOf('W');
Get the part of the text starting from position 6 to the end.
Java
String text = "Hello World";
String part = text.substring(6);
Sample Program

This program shows common string methods: checking if text contains a word, changing to uppercase, finding a letter's position, getting a part of the text, and replacing words.

Java
public class Main {
    public static void main(String[] args) {
        String message = "Java is fun!";

        // Check if message contains "fun"
        boolean containsFun = message.contains("fun");
        System.out.println("Contains 'fun': " + containsFun);

        // Convert message to uppercase
        String upperMessage = message.toUpperCase();
        System.out.println("Uppercase: " + upperMessage);

        // Find position of 'i'
        int indexI = message.indexOf('i');
        System.out.println("Position of 'i': " + indexI);

        // Get substring from position 5
        String sub = message.substring(5);
        System.out.println("Substring from 5: " + sub);

        // Replace 'fun' with 'awesome'
        String replaced = message.replace("fun", "awesome");
        System.out.println("Replaced text: " + replaced);
    }
}
OutputSuccess
Important Notes

String positions start at 0, so the first letter is at position 0.

Methods like substring do not change the original string but return a new one.

Strings in Java are case sensitive, so "Fun" and "fun" are different.

Summary

Use string methods to easily check, change, or find parts of text.

Common methods include contains, toUpperCase, indexOf, substring, and replace.

Remember that strings do not change themselves; methods return new strings.

Practice

(1/5)
1. Which Java String method checks if a string contains a specific sequence of characters?
easy
A. equals()
B. contains()
C. substring()
D. replace()

Solution

  1. Step 1: Understand the purpose of contains()

    The contains() method checks if the string includes a specific sequence of characters and returns true or false.
  2. Step 2: Compare with other methods

    equals() checks full equality, substring() extracts part of a string, and replace() changes characters. None check for containment.
  3. Final Answer:

    contains() -> Option B
  4. Quick Check:

    Check substring presence = contains() [OK]
Hint: Use contains() to check if text is inside a string [OK]
Common Mistakes:
  • Confusing contains() with equals()
  • Using substring() to check presence
  • Thinking replace() checks for text
2. Which of the following is the correct syntax to convert a string str to uppercase in Java?
easy
A. str.toUpperCase();
B. str.uppercase();
C. toUpperCase(str);
D. str.toUpper();

Solution

  1. Step 1: Recall the method name for uppercase conversion

    The correct method name in Java String class is toUpperCase(), called on the string object.
  2. Step 2: Check syntax correctness

    str.toUpperCase(); uses str.toUpperCase(); which is valid. Others use incorrect method names or syntax.
  3. Final Answer:

    str.toUpperCase(); -> Option A
  4. Quick Check:

    Uppercase method = toUpperCase() [OK]
Hint: Remember Java String methods use camelCase like toUpperCase() [OK]
Common Mistakes:
  • Using uppercase() instead of toUpperCase()
  • Calling method without object (toUpperCase(str))
  • Using toUpper() which does not exist
3. What is the output of the following Java code?
String text = "Hello World";
int pos = text.indexOf("World");
System.out.println(pos);
medium
A. 6
B. 0
C. -1
D. 5

Solution

  1. Step 1: Understand what indexOf() does

    The indexOf() method returns the starting index of the substring if found, or -1 if not found.
  2. Step 2: Find the position of "World" in "Hello World"

    "Hello " has 6 characters (index 0 to 5), so "World" starts at index 6.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Substring start index = 6 [OK]
Hint: indexOf() returns first position of substring or -1 if missing [OK]
Common Mistakes:
  • Confusing index with length
  • Expecting index 0 for substring
  • Thinking indexOf() returns boolean
4. Identify the error in this Java code snippet:
String s = "Java Programming";
String part = s.substring(5, 3);
System.out.println(part);
medium
A. substring() cannot be used on String objects
B. Missing semicolon after substring() call
C. substring() arguments are reversed; start index must be <= end index
D. The variable 'part' is not declared

Solution

  1. Step 1: Check substring() method argument rules

    substring(start, end) requires start index less than or equal to end index; here 5 > 3, which is invalid.
  2. Step 2: Confirm other syntax correctness

    Semicolons are present, substring() is valid on String, and 'part' is declared properly.
  3. Final Answer:

    substring() arguments are reversed; start index must be <= end index -> Option C
  4. Quick Check:

    substring(start, end) needs start ≤ end [OK]
Hint: Start index must not be greater than end index in substring() [OK]
Common Mistakes:
  • Swapping start and end indexes
  • Thinking substring() changes original string
  • Ignoring exception thrown at runtime
5. Given the string input = "apple,banana,orange", which code snippet correctly replaces all commas with semicolons and converts the result to uppercase?
hard
A. input.replaceFirst(",", ";").toUpperCase();
B. input.toUpperCase().replaceFirst(",", ";");
C. input.replace(",", ";").toUpper();
D. input.replaceAll(",", ";").toUpperCase();

Solution

  1. Step 1: Understand replace() vs replaceAll()

    Both replace() and replaceAll() can replace characters, but replaceAll() uses regex and is safer for multiple replacements.
  2. Step 2: Check method chaining and correctness

    A and B use replaceFirst() which only replaces the first comma. C uses invalid toUpper().
  3. Step 3: Choose best practice

    Using replaceAll() with chaining to toUpperCase() is correct and clear.
  4. Final Answer:

    input.replaceAll(",", ";").toUpperCase(); -> Option D
  5. Quick Check:

    Replace commas then uppercase = replaceAll + toUpperCase() [OK]
Hint: Chain replaceAll() then toUpperCase() for clean transformation [OK]
Common Mistakes:
  • Using toUpper() instead of toUpperCase()
  • Using replaceFirst() which only replaces the first occurrence
  • Confusing replace() and replaceAll() usage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard