Solved Finding a way to replace chars

Discussion in 'Spigot Plugin Development' started by Ean244, Jun 2, 2017.

  1. Hi i was trying to get a string from my config, i need to replace some characters in the to something else because they just acts as placeholders.I tried using .replace(CharSequence, CharSequence) but it gave an exeption.Does anyone have an alternative to this problem?
     
  2. Code (Text):
    string.replaceAll("%player%", player.getName());
    Above example replaces "%player%" with the players name in the chosen string.
     
    • Funny Funny x 2
  3. Maybe show the exception with us?
     
  4. @Ean244 simple solution is just to use String#replaceAll("find this", "replace with");
     
  5. Thanks problem fixed should have used replaceall
     
  6. @ondrej008 i have a long value named money i used replaceAll(%money%, money.toString()) but it gave me a nullpointerexe
     
  7. post the code and the stacktrace please.
     
  8. Nvm found the problem
     
  9. Lock and mark as solved, plz
     
  10. replaceAll is for regex, not literal Strings. This means that replaceAll is not the right solution, but replace should be.

    Instead, post the error replace gave you, and we can solve it together.
     
    • Agree Agree x 3
  11. I know, but String#replaceAll() is still a simple solution :p
    I never got String#replace() to work tbh.
    Also Seraphim, how do I stop a runnable? It's a TaskTimer and return; and cancel(); dont work.
    I tried throwing a custom Exception but it can't compile, because it's 'unhandled', if I 'handle' it, then it doesn't stop the runnable.
     
  12. ^ Try creating a condition quickly that you knwo will be true to see if it cancels
     
    • Like Like x 1
  13. OMG THANK YOU!!!
    The weird thing is the cancel and the return were already in an if... but creating a new if fixed it... wth?
     
  14. :O There ya go and good luck :)
     
  15. String replace is even easier, since it won't have to compile the first parameter to regex (which is less error prone)
     
    • Agree Agree x 1
  16. Please close the thread. ;)
     
  17. Choco

    Moderator

    Is there any reason as to why everyone in this thread is ignoring @DarkSeraphim? He's absolutely right... #replaceAll() takes your replacement parameter and compiles it into a RegEx pattern, which can cause unintended behaviour as well as minor efficiency decreases due to the compilation of RegEx. #replace() is what you want because it needs not compile a RegEx pattern in order to replace values. The #replaceAll() method is poorly named. Don't use it.

    If you're receiving an exception, it's best that we see it rather than letting us guess instead. We're not magicians and cannot assume what you did incorrectly. Your thread is not solved. It was temporarily resolved with poorer and less intelligent answers from people who are clearly not informed about the difference between the two methods mentioned above.
     
    • Agree Agree x 1
  18. i solved it thanks for ur help anyway big guy
    i actually used his method(replaceall)
     
    • Funny Funny x 1
  19. Choco

    Moderator

    [​IMG]

    okay...
     
    • Funny Funny x 5
    • Agree Agree x 1
  20. thanks to all for your help. i have solved this problem as shown on title i dont think we have a point to discuss about it anymore