Solved Making strings pretty

Discussion in 'Plugin Development' started by 1Camer0471, Sep 16, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    1Camer0471

    So I'm trying to make a ranks plugin for my server, and the code I'm runnig works perfect for all the ranks except ones with '+' in them.

    Code:JAVA
    1.  
    2. public String getSimpleName() {
    3. System.out.println(name);
    4. for (char c : name.toCharArray()) {
    5. if (Character.isAlphabetic(c) == false && c != '+') {
    6. System.out.println(c);
    7. name = name.replace(Character.toString(c), "");
    8. }
    9. if (c == '§' || c == 'º') {
    10. System.out.println(c);
    11. System.out.println(Character.toString(name.charAt(name
    12. .indexOf(c) + 1)));
    13. name = name.replace(
    14. Character.toString(name.charAt(name.indexOf(c) + 1)),
    15. "");
    16. name = name.replace("º", "");
    17. }
    18. }
    19. System.out.println(name);
    20. return name;
    21. }
    22.  


    Output when name = "MVP" and then name = "MVP+"

    Code:
    [20:33:45 INFO]: ºb[MVP]
    [20:33:45 INFO]: º
    [20:33:45 INFO]: º
    [20:33:45 INFO]: b
    [20:33:45 INFO]: [
    [20:33:45 INFO]: ]
    [20:33:45 INFO]: MVP
    [20:33:45 INFO]: ºb[MVPº4+ºb]
    [20:33:45 INFO]: º
    [20:33:45 INFO]: º
    [20:33:45 INFO]: b
    [20:33:45 INFO]: [
    [20:33:45 INFO]: º
    [20:33:45 INFO]: º
    [20:33:45 INFO]: M
    [20:33:45 INFO]: 4
    [20:33:45 INFO]: º
    [20:33:45 INFO]: º
    [20:33:45 INFO]: V
    [20:33:45 INFO]: ]
    [20:33:45 INFO]: P+
     
  2. Offline

    Zombie_Striker

    @1Camer0471
    The fact that you post the output, but don't leave any tags to the output (e.g. Name : MVP+, Letter 12 = v, New Line ect.) make it hard to tell what is going on.

    Also, it would be easier (and more efficent) if you just used
    Code:
    name = name.replace("Char1","").replace("char2","")...
    instead of doing useless checks and using methods for replacing chars that use up much more memory than what is needed.
     
    1Camer0471 likes this.
  3. Offline

    FabeGabeMC

    use:
    Code:
    !Character.isAlphabetic(c)
    as Character.isAlphabetic(c) is a boolean and will return true/false.
     
    1Camer0471 likes this.
  4. Offline

    1Camer0471

    @Zombie_Striker The check is not useless though, since I'm using color codes so .isAlphabetic(c) would not catch those.

    Never Mind, I figured it out, I'm supposed to us .replaceFirst() since .replace() removes ALL instances of that char in the string so it removed all the color code chars at once but then left the letter for it behind.

    Thanks for your help!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
Thread Status:
Not open for further replies.

Share This Page