Solved Convert Arguements to string

Discussion in 'Plugin Development' started by puppy3276, Nov 17, 2012.

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

    puppy3276

    Lets say the player types: /setpluginconfig test Hello Everyone at Bukkit!
    I want it to set the test value in the config (Which is already working), to Hello Everyone at Bukkit by merging all arguments after test into a string. How would I do this?
     
  2. Offline

    fireblast709

    Code:java
    1. if(args > 0)
    2. {
    3. String key = args[0];
    4. value = null;
    5. if(args > 1)
    6. {
    7. StringBuilder sb = new StringBuilder();
    8. for(int i = 1; i < args.length + 1; i++)
    9. {
    10. sb.append(args[i]);
    11. }
    12. value = sb.toString();
    13. }
    14. // From now, there are 2 cases: value = null and value = "some string"
    15. // In case value = null, the key would be removed (you could catch this and set it to default
    16. // In case valie = "some string", the key would be set to value "some string"
    17. }[/i]
     
  3. Offline

    cman1885

    Or just do a for(String s: args);
     
  4. Offline

    one4me

    You've got a couple static methods you can use.
    Code:
    StringUtils.join(args, ' ', startIndex, args.length);
    Joiner.on(' ').join(args);
    
    Or you can make your own method:
    Code:
    public String buildArg(String[] input, int startArg, int endArg) {
      if(input.length <= startArg || startArg > endArg) {
        return "";
      }
      StringBuilder sb = new StringBuilder(input[startArg]);
      for(startArg++; startArg < endArg && startArg < input.length; startArg++) {
        sb.append(' ').append(input[startArg]);
      }
      return sb.toString();
    }
    
     
  5. Offline

    fireblast709

    You would need to extract the first argument first, if you want to do that. (As his first argument is the key).
     
  6. Offline

    cman1885

    Oh, didn't see that. Nevermind.
     
Thread Status:
Not open for further replies.

Share This Page