Regular Expressions
Declarative Programming
Advanced Language Features
Programming Paradigms
Fundamentals
Strings
With the power of regex it becomes easy to tell if your string has even length!
class StringParity {
public static boolean isEvenLength(String str) {
return str.replaceAll("(?s)..", "").isEmpty(); // (?s) enables DOTALL
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SolutionTest {
@Test
void testSomething() {
assertEquals(true, StringParity.isEvenLength("even"));
assertEquals(false, StringParity.isEvenLength("odd"));
assertEquals(true, StringParity.isEvenLength(""));
assertEquals(false, StringParity.isEvenLength("Instant Noodles"));
assertEquals(true, StringParity.isEvenLength("\n\n"));
}
}