Strings for Test Data

Methods You Will Use Daily

MethodUse in tests
equals / equalsIgnoreCasecompare the actual text with the expected text
containstitle contains "ShopEase"
trimremove spaces around text read from the page
replace / replaceAllturn "₹1,299" into "1299"
splitbreak "Showing 1-24 of 120" into parts
startsWith / endsWithURL starts with https

Example

Example

Cleaning text read from a product card and converting the price into a number:

public class Main {
    public static void main(String[] args) {
        String title = "  Nike Revolution 7 Running Shoes  ";
        String priceText = "₹1,299.00";
        String resultText = "Showing 1-24 of 120 results";

        System.out.println("[" + title.trim() + "]");
        System.out.println("Contains Running: " + title.contains("Running"));

        double price = Double.parseDouble(priceText.replace("₹", "").replace(",", ""));
        System.out.println("Price as number: " + price);

        String[] words = resultText.split(" ");
        int total = Integer.parseInt(words[3]);
        System.out.println("Total results: " + total);
        System.out.println("Same text? " + "Add to Cart".equalsIgnoreCase("ADD TO CART"));
    }
}

Output

[Nike Revolution 7 Running Shoes]
Contains Running: true
Price as number: 1299.0
Total results: 120
Same text? true

Note: Never compare strings with ==. It compares object references. Always use equals().