Skip to content

Instantly share code, notes, and snippets.

@BrentAureli
Last active September 13, 2024 04:11
Show Gist options
  • Save BrentAureli/28fbc35629336609e23e08c7b1d5537c to your computer and use it in GitHub Desktop.
Save BrentAureli/28fbc35629336609e23e08c7b1d5537c to your computer and use it in GitHub Desktop.
Exclusive and Inclusive compares when using quotes.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class StringMatcher {
public static List<String> matchInput(String input, List<String> stringList) {
List<String> matches = new ArrayList<>();
// Check if input is surrounded by quotes
if (input.startsWith("\"") && input.endsWith("\"")) {
// Remove the quotes and make case-insensitive comparison
String exactMatch = input.substring(1, input.length() - 1).toLowerCase();
for (String str : stringList) {
if (str.equalsIgnoreCase(exactMatch)) {
matches.add(str);
}
}
} else {
// Search for strings containing the input (case-insensitive)
String searchTerm = input.toLowerCase();
for (String str : stringList) {
if (str.toLowerCase().contains(searchTerm)) {
matches.add(str);
}
}
}
return matches;
}
public static void main(String[] args) {
// Use Arrays.asList for compatibility with Java 8
List<String> stringList = Arrays.asList("Dog", "Cat", "xxdogxx", "Fish", "bird");
// Test with quoted input
String inputQuoted = "\"dog\"";
System.out.println("Matches for quoted input: " + matchInput(inputQuoted, stringList));
// Test with unquoted input
String inputUnquoted = "dog";
System.out.println("Matches for unquoted input: " + matchInput(inputUnquoted, stringList));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment