Code Snippet: Substring with ChatColors

Discussion in 'Resources' started by thebigdolphin1, May 4, 2013.

Thread Status:
Not open for further replies.
  1. Here:
    Code:
        public String substringIgnoreColor(String string, int min, int max) {
            int currentLength = 0;
            String currentText = "";
            boolean wasLastSymbol = false;
     
            for(char c : string.toCharArray()) {     
                if(!(currentLength < min) && !(currentLength > max)) {
                    if(c == '§') {
                        wasLastSymbol = true;
                    } else if(wasLastSymbol) {
                        wasLastSymbol = false;
                    } else {
                        currentLength++;
                    }
                    currentText = currentText + c;
                } else {
                    if(c == '§') {
                        wasLastSymbol = true;
                    } else if(wasLastSymbol) {
                        wasLastSymbol = false;
                    } else {
                        currentLength++;
                    }
                }
            }
     
            return currentText;
        }
    So
    Code:
    String message = "§1LOL§2LOL§3LOL§4LOL";
    substringIgnoreColor(message, 0, 4)
    would return:
    Code:
    §1LOL§2LO
    whereas a normal substring would return:
    Code:
    §1LOL
     
Thread Status:
Not open for further replies.

Share This Page