Bird
Raised Fist0
Javaprogramming~10 mins

Common string methods in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Common string methods
Start with a String
↓
Call method on String
↓
Method processes String
↓
Returns result (new String, int, boolean, etc.)
↓
Use or print result
↓
End
This flow shows how you start with a string, call a method on it, get a result, and then use that result.
Execution Sample
Java
String text = "Hello World";
int length = text.length();
String upper = text.toUpperCase();
boolean contains = text.contains("lo");
System.out.println(length);
System.out.println(upper);
This code gets the length of a string, converts it to uppercase, checks if it contains "lo", and prints results.
Execution Table
StepActionExpressionResultOutput
1Assign stringtext = "Hello World""Hello World"
2Get lengthtext.length()11
3Convert to uppercasetext.toUpperCase()"HELLO WORLD"
4Check contains substringtext.contains("lo")true
5Print lengthSystem.out.println(length)11
6Print uppercaseSystem.out.println(upper)HELLO WORLD
💡 All methods executed; program ends after printing results.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
textnull"Hello World""Hello World""Hello World""Hello World""Hello World"
lengthundefinedundefined11111111
upperundefinedundefinedundefined"HELLO WORLD""HELLO WORLD""HELLO WORLD"
containsundefinedundefinedundefinedtruetruetrue
Key Moments - 3 Insights
Why does calling toUpperCase() not change the original string?
Because strings in Java are immutable, toUpperCase() returns a new string (see step 3 in execution_table), original 'text' stays the same.
What does length() return exactly?
length() returns the number of characters in the string, including spaces (step 2 shows length is 11 for "Hello World").
How does contains() work with substrings?
contains() checks if the exact sequence of characters exists in the string (step 4 shows it returns true for "lo" in "Hello World").
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'length' after step 2?
A11
B10
Cundefined
D12
💡 Hint
Check the 'Result' column for step 2 in execution_table.
At which step does the string become uppercase?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for toUpperCase() method call in execution_table.
If we changed the substring in contains() to "xyz", what would the result be at step 4?
Atrue
Bfalse
Cnull
DException
💡 Hint
contains() returns true only if substring exists; check step 4 result for "lo".
Concept Snapshot
Common String Methods in Java:
- length() returns number of characters
- toUpperCase() returns uppercase copy
- contains(sub) checks if substring exists
Strings are immutable; methods return new values
Use methods by calling on string variable like text.method()
Full Transcript
This visual execution shows how common string methods work in Java. We start with a string variable 'text' holding "Hello World". We call length() to get the number of characters, which is 11. Then we call toUpperCase() which returns a new string "HELLO WORLD" without changing the original. Next, contains("lo") checks if "lo" is inside the string and returns true. Finally, we print the length and uppercase string. Variables track these values step-by-step. Key points include that strings do not change when calling methods like toUpperCase(), length counts all characters including spaces, and contains checks for exact substrings. The quiz questions help confirm understanding of these steps.

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