Command Arguments contained in quotes

Discussion in 'Plugin Development' started by Yourdogsdead, Sep 1, 2012.

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

    Yourdogsdead

    Hello, my plugin lets a person to write a message at the end of the the command. Unfortunately, it is being limited to a single word. I tried passing it as multiple words surrounded by quotation marks. ie "Hello World", but it keeps only passing the "Hello part. Is there a different way of handling this. Thanks for any help.
     
  2. Offline

    kyle1320

    Yourdogsdead
    Code:
    String message = "";
    for (String s : args){
        message += (s + " ");
    }
     
  3. Offline

    escortkeel

    A better way to do that is:

    Code:
    StringBuilder text = new StringBuilder();
    for(String part : args) {
        text.append(part);
    }
    Then use text.toString() to get the message.
     
  4. Offline

    _Waffles_

    Can I ask why that is better?
     
  5. Offline

    escortkeel

    Ah, well, the "+" operator on strings implicitly calls String.append(otherString). That creates a new object every time (specifically a useless char array) and therefore induces a slight (incredibly, incredibly small) inefficiency.
     
    CorrieKay likes this.
  6. Offline

    MrFigg

    Try this:
    Code:
    public function combineArgs(String[] args) {
        boolean quoteOpened = false;
        String combinedArg = null;
        ArrayList<String> returnedArgs = new ArrayList<String>();
        for(int i = 0; i < args.length; i++) {
            if(args[i].startsWith("\"")) {
                quoteOpened = true;
                args[i] = args[i].subString(1);
            }
            if(!quoteOpened) {
                returnedArgs.add(args[i]);
            } else {
                if(combinedArg==null) combinedArg = "";
                if(combinedArg.length>0) combinedArg += " ";
                combinedArg += args[i];
            }
            if(args[i].endsWith("\"")) {
                quoteOpened = false;
                returnedArgs.add(combinedArg.subString(0, -1);
                combinedArg = null;
                args[i] = args[i].subString(0, -1);
            }
        }
        if(quoteOpened) return args;
        return returnedArgs.toArray(new String[]{});
    }
    Wrote it in notepad so it's untested.
     
  7. Offline

    escortkeel

    Well that's a little bit unnecessary. :p
     
  8. Offline

    Yourdogsdead

    Thank you guys for all the suggestions. I am rewriting my plugins so it doesn't take 2 minutes to perform one operation. I will implement some of your suggestions after that. Thank you!
     
Thread Status:
Not open for further replies.

Share This Page