Combine all command arguments into one string.

Discussion in 'Plugin Development' started by chaseoes, Jul 21, 2012.

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

    chaseoes

    I'm pretty sure Digi posted a useful way to do this a while back. I'm currently using this:
    Code:Java
    1. String thefinalstring = "";
    2. for (int i = 2; i < strings.length - 1; i++) {
    3. thefinalstring += strings[i] + ' ';
    4. }
    5. thefinalstring += strings[strings.length - 1];[I][/I][/i]

    Any shorter/faster/better way to do it?
     
  2. Offline

    r0306

    chaseoes
    If you're talking about a command, you could just use this:
    Code:
    String string = cmd.getName() + " " + Arrays.toString(args);
     
  3. Offline

    CorrieKay

    instead of line five, just do this:
    thefinalstring = thefinalstring.trim();
     
  4. Offline

    chaseoes

    Thanks!
     
  5. Offline

    r0306

  6. Offline

    CorrieKay

    that would come out as "commandname [arg1, arg2, arg3]"
     
  7. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Use a stringbuilder for minimal object creation.
     
  8. That ^
    Code:
    StringBuilder buffer = new StringBuilder();
    
    // change the starting i value to pick what argument to start from
    // 1 is the 2nd argument.
    for(int i = 1; i < args.length; i++)
    {
        buffer.append(' ').append(args[i]);
    }
    
    // now buffer.toString() has your arguments separated by spaces.

    That will make all arguments glued... let's say you type /cmd player set heal, that code of yours will print "cmd playersetheal" :}
     
  9. Offline

    CorrieKay

    Thats what i thought at first. But apparently that specific method (Arrays.toString(Array)) prints out "[args... ]"
     
  10. Ah yes, it's a way to print array values, not to join them.
    So, correction, "/cmd player set heal" with Arrays.toString(args) will print "[player, set, heal]" xD

    r0306
    Yes but it will still have the ",", and I belive it's slower this way, as Arrays.toString() already loops through the items, adding a .substring() method would be slower.


    Also, I just remembered there's a Joiner class (static methods, from com.google package) which can be used to join stuff together with custom separator :} example usage:
    Code:
    String joined args = Joiner.on(' ').join(args);
    It can also have some customizable options as well... like .skipNulls(), .withKeyValueSeparator(), etc.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
    TGamingStudio likes this.
  11. Offline

    r0306

    Digi
    chaseoes
    Ahh. Here we go:
    Code:
    String string = Arrays.toString(string).replace("[", "").replaceAll("[],,]", "");
     
  12. Even more string objects... even slower :}

    So, for speed and efficiency (what the OP requested) the stringbuilder + loop way is the best in my opinion.
     
  13. Offline

    r0306

    Digi
    The difference is so small that it can be disregarded in this situation. :)
     
  14. Offline

    Sagacious_Zed Bukkit Docs

    Guava joiner is fairly short too, and i normally use it on one line by itself.
    Code:
    String s = Joiner.on(" ").join(args);
    where args is the array of strings arguments.
     
Thread Status:
Not open for further replies.

Share This Page