Splitting strings at every character?

Discussion in 'Plugin Development' started by afistofirony, Mar 12, 2013.

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

    afistofirony

    Hey! I'm making a command in which every letter of a certain argument does something (an example would be /tp -oh fist to force a teleport teleport of me to the command sender - "o" forces a teleport, "h" means 'here').

    But moving on to my question, is it possible to doString[] string = args[i]split("")to split at every character (and then be able to use a for loop to check if there is a certain letter in that argument), or would that throw an error?

    (example):
    Code:java
    1.  
    2. if(args.startsWith("-")){
    3. String[ ] variables = args.split("");
    4. for(String modifier : variables){
    5. if(modifier.equalsIgnoreCase("o")){
    6. // do stuff
    7. }
    8. if(modifier.equalsIgnoreCase("h")){
    9. // do stuff
    10. }
    11. }
    12. }


    The reason why I want to use this method is because I want to be able to use the argument at any point within the command as opposed to in a specific order (but it can be difficult to do so with .contains()).

    Thanks in advance!

    EDIT 5 ( :mad: ): Fixed italicization error
     
  2. Offline

    Tirelessly

    String.toCharArray()
     
    afistofirony likes this.
  3. Offline

    ZeusAllMighty11

    Code:
    String str = "Zeus";
    char[] cArray = str.toCharArray();
    
    Output should be "Z, e, u, s" if you printed the list, otherwise you can use cArray.getCharAt[0] for Z, [1] for e, etc I think

    Edit: dammit lol ninja'd by Tirelessly :p
     
    afistofirony likes this.
  4. Offline

    afistofirony

    Alright, thanks! :)
     
Thread Status:
Not open for further replies.

Share This Page