Answer
Important Java String Methods
Java
public class StringMethods {
public static void main(String[] args) {
String s = " Automation Testing with Selenium ";
// Length
System.out.println("Length: " + s.length()); // 36
// Trim whitespace
String trimmed = s.trim();
System.out.println("Trimmed: '" + trimmed + "'");
// Upper and Lower Case
System.out.println("Upper: " + trimmed.toUpperCase());
System.out.println("Lower: " + trimmed.toLowerCase());
// charAt - get character at index
System.out.println("charAt(0): " + trimmed.charAt(0)); // A
// indexOf - find position of substring
System.out.println("indexOf 'Testing': " + trimmed.indexOf("Testing")); // 11
// contains - check if substring exists
System.out.println("Contains 'Selenium': " + trimmed.contains("Selenium")); // true
// substring - extract part of string
System.out.println("substring(0,10): " + trimmed.substring(0, 10)); // Automation
// replace - replace text
System.out.println("Replace: " + trimmed.replace("Selenium", "Playwright"));
// split - split by delimiter
String csv = "Chrome,Firefox,Edge,Safari";
String[] browsers = csv.split(",");
System.out.println("Split count: " + browsers.length); // 4
for (String b : browsers) System.out.print(b + " ");
// startsWith / endsWith
System.out.println("\nstartsWith 'Auto': " + trimmed.startsWith("Auto")); // true
System.out.println("endsWith 'ium': " + trimmed.endsWith("ium")); // true
// equals vs equalsIgnoreCase
System.out.println("equals: " + "Selenium".equals("selenium")); // false
System.out.println("equalsIgnoreCase: " + "Selenium".equalsIgnoreCase("selenium")); // true
// isEmpty / isBlank
System.out.println("isEmpty: " + "".isEmpty()); // true
System.out.println("isBlank: " + " ".isBlank()); // true (Java 11+)
}
}
Output
CODE
Length: 36
Trimmed: 'Automation Testing with Selenium'
Upper: AUTOMATION TESTING WITH SELENIUM
Lower: automation testing with selenium
charAt(0): A
indexOf 'Testing': 11
Contains 'Selenium': true
substring(0,10): Automation
Replace: Automation Testing with Playwright
Split count: 4
Chrome Firefox Edge Safari
startsWith 'Auto': true
endsWith 'ium': true
equals: false
equalsIgnoreCase: true
isEmpty: true
isBlank: true
String Comparison — Common Mistake
Java
String a = new String("hello");
String b = new String("hello");
System.out.println(a == b); // FALSE — compares references
System.out.println(a.equals(b)); // TRUE — compares content
Always use .equals() to compare String content, never ==.
Automation Testing Relevance
Java
// Verify page title
String title = driver.getTitle().trim().toLowerCase();
assertTrue(title.contains("dashboard"), "Title doesn't contain dashboard");
// Extract number from text like "$1,299.00"
String priceText = element.getText().replace("$", "").replace(",", "").trim();
double price = Double.parseDouble(priceText);
