How to get all of the arguments after args[1] in a command

Discussion in 'Plugin Development' started by william9518, Dec 26, 2012.

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

    william9518

    so right now i use
    Code:
    String name = args[2];
    plugin.setDisplayName(((Player) sender).getItemInHand(), name);
    I want the name to be everything after args[1]. how can i do that? it works now,
    but it can only be 1 word with no spaces. i want "name" to be all the args after args[1]
     
  2. Offline

    MP5K

    hello william9518,
    you could use this: (i haven't tested it but i should work fine)
    PHP:
    public String allArgs(int start String[] args){
        
    String temp "";
        for(
    int i start args.length i++){
         
    temp += args[i] + " "
        }
       return 
    temp.trim();
    }
     
  3. Offline

    YoFuzzy3

    Or you could do this...
    Code:java
    1. if(args.length >= 2){
    2. StringBuilder message = new StringBuilder();
    3. for(int i = 1; i < args.length; i++){
    4. message.append(" ").append(args);
    5. }
    6. message.toString();
    7. }


    It will makes all arguments from args[ 1 ] and onwards into one String.
     
    zack6849 likes this.
  4. Offline

    keensta

    Code:
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < args.length; i++) {
                if (i > 0) sb.append(" ");
                sb.append(args[i]);
            }
    
    Use sb.toString(); to the if you need to send it to a user. I use the Space part at the top so that way no need to trim it.
    Also say you want to start on args[1] just change i = 0; to i = 1;

    Edit: I add the if (i > 0) so it doesn't add a space at the start and because its above the argument onces it put the first argument in it will put the space. So you don't get unwanted space at the front or the back then you have to trim it and all that.
     
Thread Status:
Not open for further replies.

Share This Page