Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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: <result>, equalsIgnoreCase: <result>"
return null;
boolean equalsValue = a.equals(b);
boolean equalsIgnoreCase = a.equalsIgnoreCase(b);

return "equals: " + equalsValue + ", equalsIgnoreCase: " + equalsIgnoreCase;
}

/**
Expand All @@ -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";
}

/**
Expand All @@ -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: <bool>, indexOf: <index>"
return null;
boolean hasText = text.contains(keyword);
int keywordPosition = text.indexOf(keyword);
return "contains: " + hasText + ", indexOf: " + keywordPosition;
}

/**
Expand All @@ -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", "#");
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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 '<prefix>': <bool>, endsWith '<extension>': <bool>"
return null;
boolean hasPrefix = filename.startsWith(prefix);
boolean hasExtension = filename.endsWith(extension);
return "startsWith '<" + prefix + ">': <" + hasPrefix + ">, endsWith '<" + extension + ">': <" + hasExtension + ">" ;
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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) {
Expand Down