Separate String into String and Colors Array?

Discussion in 'Plugin Development' started by NerdsWBNerds, Apr 12, 2014.

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

    NerdsWBNerds

    So I have a string, for example.

    &cC&do&fl&1o&2r

    Which would read Color in 5 different colors, I need to find out how to seperate this into two different arrays, or array lists, one containing the text, one containing the color

    So on one array, we'd have
    C
    o
    l
    o
    r

    and in the other we'd have
    c
    d
    f
    1
    2

    Does anyone know of a fast, efficient way to do this?
     
  2. NerdsWBNerds What if you had more than one letter for a colour? Like, would you want &fhe&4y to return:

    he
    y

    And

    f
    4
     
  3. Offline

    NerdsWBNerds


    Yes, and if there were multiple color codes together (examples, &fh&c&lello) which would read (hello)
    I would want it seperated and the two color codes (c - red, l - bold) seperated by commas,
    like this

    h
    ello

    f
    c,l
     
  4. NerdsWBNerds I doubt this is the best or most efficient way of doing this, but the following code should work for what you want, where s is the string you want to separate. I can tell it's not my best work, so if you're confused by any part of it let me know. :)

    PHP:
            List<Stringcolours = new ArrayList<String>();
            List<
    Stringtext = new ArrayList<String>();
           
            
    boolean b false;
            for(
    int i 0s.length(); i++) {
                if(
    s.charAt(i) == '&') continue;
                if(
    s.charAt(i-1) == '&') {
                    if(
    b) {
                        
    int index colours.size() - 1;
                        
    String element colours.get(index);
                        
    element += "," s.charAt(i);
                        
    colours.set(indexelement);
                    } else {
                        
    colours.add(""+s.charAt(i));
                        
    true;
                    }
                } else {
                    
    StringBuilder sb = new StringBuilder();
                    
    int index s.indexOf('&'i);
                    if(
    index == -1index s.length();
                    while(
    index) {
                        
    sb.append(s.charAt(i));
                        
    i++;
                    }
                    
    text.add(sb.toString());
                    
    false;
                }
            }
     
  5. Offline

    NerdsWBNerds


    Thanks man! It took a couple changes to make it work exactly the way I wanted, but still amazing! Thanks :D
     
  6. NerdsWBNerds Glad I could help. Out of interest, what did you change?
     
  7. Offline

    NerdsWBNerds

    I just through it in a try/catch because it would return an error because the minus one would make it check the -1 index which doesn't exist :p
     
  8. NerdsWBNerds Oops. Didn't think of that one. Just be careful where you put the catch, might be in a place that stops the thing from working but if you say it works then good. :)
     
  9. Offline

    AoH_Ruthless

    NerdsWBNerds AdamQpzm
    To get the word "color" or whatever your example was, you could just do ChatColor.stripColor(String);
     
  10. AoH_Ruthless Yes but that's not what he was looking to do.
     
  11. Offline

    xTrollxDudex

Thread Status:
Not open for further replies.

Share This Page