1.17.x How can I get a string containing spaces?

Discussion in 'Spigot Plugin Development' started by OHvrything, Jul 3, 2021.

  1. How can I get a string containing spaces?
  2. use this method
    Code (Text):
    public static String buildStringFromArgs(String[] args, int start) {
            String w = "";
            for(int i = start; i < args.length; i++) {
               w += args[i] + " ";
            w = w.substring(0, w.length()-1); //remove whitespace
            return w;
    Now, to get the reason, put this inside your onCommand block:
    Code (Text):
    Now, to get the reason, put this inside your onCommand block:
  3. From where? From the args[] in onCommand?
    • Agree Agree x 1
  4. The args on commands are splitted by space so if user adds multiple space it won’t work
  5. Nobody said anything about keeping extra whitespaces, but I'm pretty sure another thread recently covered that that's pretty much just not possible. I was just offering a more concise way of getting the same result that chochoco4777's snippet would have.
  6. It’s actually possible using the event that I linked, it gets the whole command as a raw message
  7. you can get the chars and check if there is a char that's a space, if it's not null then that string contains a space, else it doesn't.

    if you want to replace the space by another char you can do: string.replace(' ', new_string).
  8. I somehow missed that, but that event isn't for handling command logic and should be avoided if possible. I don't think it's worth a few extra spaces. I can't be bothered to look around myself, but someone on this thread says it's already filtered anyway.
    Anyway, that all is besides the point. If that was something OP wanted, that would be something for OP to mention.
  9. Op didn’t even mention where he gets the string from, it could be from config lol
  10. Yeah, a few assumptions were made here, but you don't need to do anything special to get strings with spaces from the config? Unless you need to fetch various placeholders (or something of the sort), but that doesn't sound like what OP's asking for in the title.
  11. What he means is, we can't help really, as this could be refered to "how to get command empty arguments".

    We can't help if the op doesn't tell us exactly what he wants. You can't help a "I want to get empty spaces of a string". Because there's no context.

    As the XY Problem says, let's imagine Joe wants to get a file extension, instead he asks how to get the last 3 characters of a file.

    Then this is exactly the same.

    OP wants X, but he asks Y.

    Until OP doesn't put us in context, all help is useless
    • Like Like x 1
  12. I'm perfectly aware of the current situation. I do not need a breakdown of it.