Count amount of characters in string without the formatting codes

Discussion in 'Spigot Plugin Development' started by Invaerne, Jul 8, 2018.

  1. Hi guys,

    I need to count the number of characters in the string without the formatting codes (&a, &6, etc).

    Could someone guide me on this case?
     
    • Informative Informative x 1
  2. Shockzeh answer is really the easiest, otherwise you can resort to String.split() or Regex.
    Caveat: This will however ignore single use cases of '&' in any string.
    Code (Java):
    StringBuilder sb = new StringBuilder();
    String unformattedString = ""
    + "Hi guys,"
    + "I need to count the number of characters in the string without the formatting codes (&a, &6, etc)."
    + "Could someone guide me on this case?"
    ;

    for (String s : unformattedString.split('&') {
        if (!s.contains('&') {
            sb.append(s);
        }
    }

    System.out.println(sb.toString().length); // print char count
     
     
    • Funny Funny x 1
    • Informative Informative x 1
  3. Thanks a lot! Just small issue: https://i.imgur.com/NASBfu8.png

    EDIT: Nevermind, that's a simple issue. shouldn't be posting that.
     
    #4 Invaerne, Jul 8, 2018
    Last edited: Jul 8, 2018
  4. For future reference you can just count the character, no need for the string builder
     
    • Useful Useful x 1
  5. Code (Java):
    int length = ChatColor.stripColor(ChatColor.translateAlternatecolorCodes('&', string)).length();
    This will correctly count the length ignoring parsed (§4) and unparsed (&4) color codes. ;)
     
    • Like Like x 1
    • Friendly Friendly x 1
    • Useful Useful x 1
  6. Not quite - It is ignoring '&' but not the character just after that.
     
  7. How so?
     
  8. Well, no- he's translating any &X code to §X, and then stripping out all of the §X's. It should do what you asked for, and also cover the case of having un-translated §X codes as well.
     
    • Like Like x 1
  9. It should, but it doesn't. :unsure:
     
    • Optimistic Optimistic x 1
  10. You did something wrong then. Show us what you tried.
     
  11. Code (Text):
                                    int length = ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', guildName)).length();
                                    Integer value = length;
                                    String lengthValue = value.toString();

                                    p.sendMessage(lengthValue);
    guildName is the argument of the command. I keep on trying &atest and it keeps on counting 5.
     
  12. Show where you set guildname.

    This works fine:
    Code (Java):
    String guildname = "&atest";
    int length = ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', guildName)).length();
    p.sendMessage(Integer.toString(length)); // outputs 4
     
    #13 BillyGalbreath, Jul 9, 2018
    Last edited: Jul 9, 2018
    • Agree Agree x 1
    • Friendly Friendly x 1
  13. You were right. Turns out the problem was my eclipse. I have create a new project, pasted the code and it works. Thanks!