Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch.fhnw.claudemartin;
- import java.util.Arrays;
- public class SomeClass {
- public static void main(final String[] args) {
- test("", "", true);
- test("12345", "12345", true);
- test("abcdefg", "abcdefg", true);
- test("abcdeXX", "abcdeYY", true);
- test("xzy", "abc", false);
- test("xzy", "abcde", false);
- test("a", "A", false);
- test("12345", "12345X", true);
- // one symbol in two chars (surrogate pair):
- String clef = "\uD834\uDD1E";
- clef = clef + clef + clef + clef + clef + clef;
- test(clef, clef + clef, true);
- // Combination with diacritical mark (acute):
- // this might have to fail. requirements unclear.
- test("é", "e\u0301", true);
- }
- static void test(final String a, final String b, final boolean expected) {
- final boolean actual = equalFirst5(a, b);
- (actual == expected ? System.out : System.err)
- .format("'%s' / '%s' = %s%n", a, b, actual);
- }
- public static boolean equalFirst5(final String a, final String b) {
- return equalFirstN(a, b, 5);
- }
- public static boolean equalFirstN(final String a, final String b, final int n) {
- if (a == b)
- return true;
- final int[] ia = a.codePoints().limit(n).toArray();
- final int[] ib = b.codePoints().limit(n).toArray();
- return Arrays.equals(ia, ib);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement