-1

Dear fellow programmers

I'm developing a Java plugin for my minecraft server. Now my issue is that I have a / in a string and want to replace it with nothing AKA "". I tried to use "/", "/", "\/" and "slash" in the first parameter of the replaceFirst method. Either didn't work.

Here is my code:

if (command.startsWith("/")) {
            command.replaceFirst("\\/", "");
            System.out.println("After replace: " + command);
            target.performCommand(command);
            sender.sendMessage("§5" + target.getName() + "§a performed the command §5/" + command);
        }

If you know how I could do this thanks for your help in advance :)

ShadowCrafter_01
  • 533
  • 4
  • 19

1 Answers1

2
  1. Strings are immutable. command.replaceFirst does not change the string that the command reference is pointing at; it creates a new string, and returns a reference to this new string. You'd have to write e.g. command = command.replaceFirst(...).

  2. Given that replaceFirst uses regexes, this is needlessly complicated. Try command = command.substring(1); instead, to lop off the first character.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72