ColoredString implements EnhancedString { public ColoredString(String s) { char[] raw = s.toCharArray(); char[] tmpPlain = new char[raw.length]; String[] tmpCodes = new String[raw.length + 1]; int textpos = 0; for (int i = 0; i < raw.length; i++) { if (i != raw.length - 1 && FORMAT_PREFIXES.indexOf(raw[i]) > -1 && COLOR_CODES.indexOf(raw[i + 1]) > -1) { if (tmpCodes[textpos] == null) { tmpCodes[textpos] = new String(raw, i, 2); } else { tmpCodes[textpos] += new String(raw, i, 2); } i++; } else if (raw[i] == CR || raw[i] == LF) { tmpCodes[textpos] = new String(raw, i, 1); tmpPlain[textpos] = ' '; textpos++; } else { tmpPlain[textpos] = raw[i]; textpos++; } } plain = Arrays.copyOf(tmpPlain, textpos); codes = Arrays.copyOf(tmpCodes, textpos + 1); } ColoredString(String s); private  ColoredString(char[] plain, String[] codes); int length(); char charAt(int i); CharSequence subSequence(int i, int j); @Override @NotNull String toString(); @Override ColoredString replaceText(Pattern p, String rText); ColoredString patternToLower(Pattern p); ColoredString patternToUpper(Pattern p); @Override boolean equals(Object obj); @Override String getRaw();  }
@Test public void testColoredString() { String testString = "&7This &9is&l the &1string&3 under test."; String plainString = "This is the string under test."; String[] codeArray = {"&7", null, null, null, null, "&9", null, "&l", null, null, null, null, "&1", null, null, null, null, null, "&3", null, null, null, null, null, null, null, null, null, null, null, null}; ColoredString cs = new ColoredString(testString); assertEquals(cs.length(), plainString.length()); assertEquals(cs.charAt(5), 'i'); assertEquals("is th", cs.subSequence(5, 10)); assertEquals(plainString, cs.toString()); assertArrayEquals(codeArray, cs.getCodeArray()); assertEquals(testString, cs.getColoredString()); assertEquals(plainString, cs.toString()); Pattern p = Pattern.compile("test"); ColoredString modified = cs.replaceText(p, "derp"); assertEquals("This is the string under derp.", modified.toString()); assertEquals("&7This &9is&l the &1string&3 under derp.", modified.getColoredString()); p = Pattern.compile("string"); modified = cs.replaceText(p, "&3r&4a&5i&6n&7b&8o&9w"); assertEquals("This is the rainbow under test.", modified.toString()); assertEquals("&7This &9is&l the &1&3r&4a&5i&6n&7b&8o&9w&3 under test.", modified.getColoredString()); }