diff --git a/src/main/java/com/amigoscode/_2_developers/_6_strings/StringMethods.java b/src/main/java/com/amigoscode/_2_developers/_6_strings/StringMethods.java index eebc0f4..c2bc893 100644 --- a/src/main/java/com/amigoscode/_2_developers/_6_strings/StringMethods.java +++ b/src/main/java/com/amigoscode/_2_developers/_6_strings/StringMethods.java @@ -2,7 +2,6 @@ /** * String Methods Exercises - * * Practice the most commonly used String methods in Java. Strings are immutable * in Java — every method returns a NEW string rather than modifying the original. */ @@ -19,7 +18,10 @@ public class StringMethods { public static String compareEquality(String a, String b) { // TODO: 1 - Use equals() and equalsIgnoreCase() to compare a and b. // Return a string in the format: "equals: , equalsIgnoreCase: " - return null; + boolean equalsValue = a.equals(b); + boolean equalsIgnoreCase = a.equalsIgnoreCase(b); + + return "equals: " + equalsValue + ", equalsIgnoreCase: " + equalsIgnoreCase; } /** @@ -33,7 +35,8 @@ public static String compareEquality(String a, String b) { public static String compareLexicographic(String a, String b) { // TODO: 2 - Use a.compareTo(b) and return: // "before" if result < 0, "equal" if result == 0, "after" if result > 0. - return null; + int result = a.compareTo(b); + return result < 0 ? "before" : result == 0 ? "equal" : "after"; } /** @@ -48,7 +51,9 @@ public static String searchString(String text, String keyword) { // TODO: 3 - Use contains() to check if text contains keyword. // Use indexOf() to find the position of keyword in text. // Return "contains: , indexOf: " - return null; + boolean hasText = text.contains(keyword); + int keywordPosition = text.indexOf(keyword); + return "contains: " + hasText + ", indexOf: " + keywordPosition; } /** @@ -64,7 +69,8 @@ public static String replaceDemo(String text, String oldWord, String newWord) { // TODO: 4 - First use replace(oldWord, newWord) to swap words. // Then use replaceAll("\\d", "#") to replace all digits with "#". // Return the final result. - return null; + String newString = text.replace(oldWord, newWord); + return newString.replaceAll("\\d", "#"); } /** @@ -79,7 +85,12 @@ public static String splitDemo(String text, String delimiter) { // TODO: 5 - Use text.split(delimiter) to get an array of parts. // Build a result string with each part on a new line: "[i] part" // Example: "[0] apple\n[1] banana\n[2] cherry" - return null; + String[] splitText = text.split(delimiter); + String resultString = ""; + for (int i = 0; i < splitText.length; i++) { + resultString = resultString + " [" + i + "] " + splitText[i] + "\n"; + } + return resultString; } /** @@ -93,7 +104,9 @@ public static String splitDemo(String text, String delimiter) { public static String checkStartEnd(String filename, String prefix, String extension) { // TODO: 6 - Use startsWith(prefix) and endsWith(extension). // Return: "startsWith '': , endsWith '': " - return null; + boolean hasPrefix = filename.startsWith(prefix); + boolean hasExtension = filename.endsWith(extension); + return "startsWith '<" + prefix + ">': <" + hasPrefix + ">, endsWith '<" + extension + ">': <" + hasExtension + ">" ; } /** @@ -107,7 +120,7 @@ public static String checkStartEnd(String filename, String prefix, String extens public static String substringDemo(String text, int beginIndex, int endIndex) { // TODO: 7 - Use text.substring(beginIndex, endIndex) to extract a portion of text. // Return the substring. - return null; + return text.substring(beginIndex, endIndex); } /** @@ -122,7 +135,7 @@ public static String substringDemo(String text, int beginIndex, int endIndex) { public static String formatReceipt(String item, int quantity, double price) { // TODO: 8 - Use String.format() to create a formatted string. // Format: "%-15s x%-5d $%.2f" (left-align item in 15 chars, quantity in 5, price with 2 decimals) - return null; + return String.format("%-15s x%-5d $%.2f", item, quantity, price); } public static void main(String[] args) {