[Tutorial] Split Strings

Discussion in 'Resources' started by FisheyLP, Apr 17, 2014.

?

Was this helpful?

  1. Yeah!

    2 vote(s)
    22.2%
  2. Nope

    2 vote(s)
    22.2%
  3. Already knew that

    5 vote(s)
    55.6%
Thread Status:
Not open for further replies.
  1. This is my 2nd. tutorial i hope you like it:
    Splitting is when you have e.g this String: "Hello my name is FisheyLP", and you want to split it on each whitespace.
    Code:Java
    1.  
    2. //The String
    3. String bla = "Hello my name is FisheyLP";
    4.  
    5. //Now we split it on each whitespace (you can too use e.g this or other symbols ---> _).
    6. String[] splittedBla = bla.split(" ");
    7.  
    8. //the stringlist contains now the following words: Hello, my, name, is and FisheyLP.
    9.  
    10. //Lets say i want to get the word name out of the stringlist.
    11. String name = splittedBla[2];
    12.  
    13. //Its similar to the args[] in the method onCommand.
    14. //Hello = splittedBla[0]
    15. //my = splittedBla[1]
    16. //name = splittedBla[2]
    17. //is = splittedBla[3]
    18. //FisheyLP = splittedBla[4]
    19.  

    *Hm... I want to make a /give command with metadata but dont know how*
    Try it like this: ;)

    Code:Java
    1.  
    2. if(commandLabel.equalsIgnoreCase("give")){
    3. if(args.length<1){
    4. return false;
    5. }else{
    6. try{
    7. String[] idAndData = args[0].split(":");
    8. int id = Integer.parseInt(idAndData[0]);
    9. int meta = Integer.parseInt(idAndData[1]);
    10. ItemStack item = new ItemStack(id, 64, (short) 1, (byte) data);
    11.  
    12. //id=ItemID, (short) 1 = itemDamage, (byte) data = the number behind the :
    13. p.getInventory().addItem(item);
    14. }catch(Exception e){
    15.  
    16. }
    17. }
    18. }
    19.  
     
    Orange Tabby likes this.
  2. Offline

    epicfacecreeper

    This isn't really a tutorial, it's just a basic Java function, and not even an obscure one.
     
    Garris0n likes this.
  3. Offline

    Not2EXceL

    And not to mention FisheyLP failed to state that String#split(String) takes regex as an input.
     
    TigerHix, Comphenix and Garris0n like this.
  4. Offline

    macguy8

    Not2EXceL
    Although true, someone who has to look up how to split strings is in no position to learn regex.
     
  5. Offline

    Garris0n

    But they should know that it takes regex, otherwise they'll be confused when they accidentally input a regex function and it doesn't work how they thought it would.
     
  6. Offline

    Comphenix

    You should at least mention it, or suggest a different API that doesn't use regular expressions at all (here using Guava, which is built-in into Bukkit):
    Code:java
    1. List<String> split = Splitter.on(" ").splitToList("This is a test.");

    Alternatively, you can escape the delimiter:
    Code:java
    1. "A string".split(Pattern.quote(" "))
     
    TigerHix, ccrama and Garris0n like this.
Thread Status:
Not open for further replies.

Share This Page