Java String Methods & compareTo() Explained

Day 22: Built-in Methods in Strings & compareTo() in Java 🔤 Today’s learning focused on powerful String built-in methods and how Java performs string comparison using compareTo(). 🔹 Built-in Methods in String Class Java provides many useful methods to manipulate strings: length() → Returns the length of the string charAt(index) → Returns character at specified index substring(start, end) → Extracts part of a string indexOf() → Returns first occurrence index lastIndexOf() → Returns last occurrence index replace() → Replaces characters replaceAll() → Replaces using regex toLowerCase() → Converts to lowercase toUpperCase() → Converts to uppercase trim() → Removes leading & trailing spaces split() → Splits string into array 📌 These methods make string manipulation easy and efficient in Java. 🔹 String Comparison using compareTo() The compareTo() method compares two strings lexicographically (alphabetical order) based on ASCII/Unicode values. It performs a three-way comparison: ✅ Returns 0 → if both strings are equal ➕ Returns positive value → if first string is greater ➖ Returns negative value → if first string is smaller ✨ Unlike equals() (two-way comparison), compareTo() helps in sorting strings in ascending or descending order. #Day22 #Java #StringsInJava #CompareTo #CoreJava #Programming #LearningJourney #TAPAcademy

  • graphical user interface, text, application, email

To view or add a comment, sign in

Explore content categories