22
33/**
44 * String Methods Exercises
5- *
65 * Practice the most commonly used String methods in Java. Strings are immutable
76 * in Java — every method returns a NEW string rather than modifying the original.
87 */
@@ -19,7 +18,10 @@ public class StringMethods {
1918 public static String compareEquality (String a , String b ) {
2019 // TODO: 1 - Use equals() and equalsIgnoreCase() to compare a and b.
2120 // Return a string in the format: "equals: <result>, equalsIgnoreCase: <result>"
22- return null ;
21+ boolean equalsValue = a .equals (b );
22+ boolean equalsIgnoreCase = a .equalsIgnoreCase (b );
23+
24+ return "equals: " + equalsValue + ", equalsIgnoreCase: " + equalsIgnoreCase ;
2325 }
2426
2527 /**
@@ -33,7 +35,8 @@ public static String compareEquality(String a, String b) {
3335 public static String compareLexicographic (String a , String b ) {
3436 // TODO: 2 - Use a.compareTo(b) and return:
3537 // "before" if result < 0, "equal" if result == 0, "after" if result > 0.
36- return null ;
38+ int result = a .compareTo (b );
39+ return result < 0 ? "before" : result == 0 ? "equal" : "after" ;
3740 }
3841
3942 /**
@@ -48,7 +51,9 @@ public static String searchString(String text, String keyword) {
4851 // TODO: 3 - Use contains() to check if text contains keyword.
4952 // Use indexOf() to find the position of keyword in text.
5053 // Return "contains: <bool>, indexOf: <index>"
51- return null ;
54+ boolean hasText = text .contains (keyword );
55+ int keywordPosition = text .indexOf (keyword );
56+ return "contains: " + hasText + ", indexOf: " + keywordPosition ;
5257 }
5358
5459 /**
@@ -64,7 +69,8 @@ public static String replaceDemo(String text, String oldWord, String newWord) {
6469 // TODO: 4 - First use replace(oldWord, newWord) to swap words.
6570 // Then use replaceAll("\\d", "#") to replace all digits with "#".
6671 // Return the final result.
67- return null ;
72+ String newString = text .replace (oldWord , newWord );
73+ return newString .replaceAll ("\\ d" , "#" );
6874 }
6975
7076 /**
@@ -79,7 +85,12 @@ public static String splitDemo(String text, String delimiter) {
7985 // TODO: 5 - Use text.split(delimiter) to get an array of parts.
8086 // Build a result string with each part on a new line: "[i] part"
8187 // Example: "[0] apple\n[1] banana\n[2] cherry"
82- return null ;
88+ String [] splitText = text .split (delimiter );
89+ String resultString = "" ;
90+ for (int i = 0 ; i < splitText .length ; i ++) {
91+ resultString = resultString + " [" + i + "] " + splitText [i ] + "\n " ;
92+ }
93+ return resultString ;
8394 }
8495
8596 /**
@@ -93,7 +104,9 @@ public static String splitDemo(String text, String delimiter) {
93104 public static String checkStartEnd (String filename , String prefix , String extension ) {
94105 // TODO: 6 - Use startsWith(prefix) and endsWith(extension).
95106 // Return: "startsWith '<prefix>': <bool>, endsWith '<extension>': <bool>"
96- return null ;
107+ boolean hasPrefix = filename .startsWith (prefix );
108+ boolean hasExtension = filename .endsWith (extension );
109+ return "startsWith '<" + prefix + ">': <" + hasPrefix + ">, endsWith '<" + extension + ">': <" + hasExtension + ">" ;
97110 }
98111
99112 /**
@@ -107,7 +120,7 @@ public static String checkStartEnd(String filename, String prefix, String extens
107120 public static String substringDemo (String text , int beginIndex , int endIndex ) {
108121 // TODO: 7 - Use text.substring(beginIndex, endIndex) to extract a portion of text.
109122 // Return the substring.
110- return null ;
123+ return text . substring ( beginIndex , endIndex ) ;
111124 }
112125
113126 /**
@@ -122,7 +135,7 @@ public static String substringDemo(String text, int beginIndex, int endIndex) {
122135 public static String formatReceipt (String item , int quantity , double price ) {
123136 // TODO: 8 - Use String.format() to create a formatted string.
124137 // Format: "%-15s x%-5d $%.2f" (left-align item in 15 chars, quantity in 5, price with 2 decimals)
125- return null ;
138+ return String . format ( "%-15s x%-5d $%.2f" , item , quantity , price ) ;
126139 }
127140
128141 public static void main (String [] args ) {
0 commit comments