If you do not want to create UUIDs, just keep it clean
String.format("%s_%s_%s_%s", str1.length(), str2.length(), str1, str2);
tested with the following
Pair.of("a", ""), // 'a' and '' into '1_0_a_' Pair.of("", "a"), // '' and 'a' into '0_1__a' Pair.of("a", "a"), // 'a' and 'a' into '1_1_a_a' Pair.of("aa", "a"), // 'aa' and 'a' into '2_1_aa_a' Pair.of("a", "aa"), // 'a' and 'aa' into '1_2_a_aa' Pair.of("_", ""), // '_' and '' into '1_0___' Pair.of("", "_"), // '' and '_' into '0_1___' Pair.of("__", "_"), // '__' and '_' into '2_1_____' Pair.of("_", "__"), // '_' and '__' into '1_2_____' Pair.of("__", "__"), // '__' and '__' into '2_2______' Pair.of("/t/", "/t/"), // '/t/' and '/t/' into '3_3_/t/_/t/' Pair.of("", "") // '' and '' into '0_0__'
This works because the beginning creates a way for parsing the next use using a separator that cannot exist with numbers; it wonβt work if you use a separator like β0β
Other examples are based on a delimiter that does not exist on any line.
str1+"."+str2 // both "..","." and ".",".." result in "...."
EDIT
Support for joining strings 'n'
private static String getUniqueString(String...srcs) { StringBuilder uniqueCombo = new StringBuilder(); for (String src : srcs) { uniqueCombo.append(src.length()); uniqueCombo.append('_'); } // adding this second _ between numbers and strings allows unique strings across sizes uniqueCombo.append('_'); for (String src : srcs) { uniqueCombo.append(src); uniqueCombo.append('_'); } return uniqueCombo.toString(); }