string to array / array to string

Discussion in 'Plugin Development' started by stelar7, Sep 10, 2011.

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

    stelar7

    This may sound very noobish, but bear with me!
    How do i split a string into a sting array?

    TL;DR:
    String msg to String[] samemsg
     
  2. Offline

    Randy Schouten

    String[] samemsn = msg.split(regex);

    The regex is where it splits.
    msg = herp, derp, flurp
    Regex = ", "
    Output:
    msg[0] = herp
    msg[1] = derp
    msg[2] = flurp
     
  3. Offline

    stelar7

    @Randy Schouten
    thanks :D

    Do you know how to put an array back to a string?

    like msg[0] + msg[1] + msg[2]...

    TL;DR:
    String array to single String
     
  4. Offline

    escape

    To convert it back to a single String, I would do something like this:
    Code:java
    1. StringBuilder sb = new StringBuilder();
    2. for(String s : yourStringArray) {
    3. sb.append(s);
    4. }
    5. String newString = sb.toString();
     
  5. Offline

    DrAgonmoray

    Code:
    public String arrayToString(String[] arr) {
        String temp;
        for (String i : arr) {
            temp+=i;
        }
        return temp;
    }
     
    That should work, but I haven't tested it.
     
  6. Offline

    escape

    @DrAgonmoray That'd work, but you'd really be creating (arr.length + 1) String objects. Not exactly memory efficient. ;)
     
  7. Offline

    stelar7

    if i say, wanted to start at the 3nd part of the array, msg[2], how would that be done?

    @escape
    Do you know of a more efficient way ?
     
  8. Offline

    escape

    @stelar7 You could still use the same method I posted but change it to:
    Code:java
    1. StringBuilder sb = new StringBuilder();
    2. for(int i = 0 /*<-- Set to the index to start at*/; i < array.length; i++) {
    3. sb.append(array.get(i));
    4. }
    5. String newString = sb.toString();
     
  9. Offline

    stelar7

    @escape
    sb.append(array.get(i));
    gives me an error...
    Cannot invoke get(int) on the array type String[]

    does sb.append(array[ i ]) work instead?
     
  10. Offline

    escape

    Whoops it should be
    Code:
    sb.append(array[i]); 
    And nope, only if it was a char array.
     
  11. Offline

    stelar7

    @escape
    that's what i tough wrote :p

    Mind telling me why this wont work?
    do i need to use OnCommand?
    Code:java
    1. public class MainPlayerListener extends PlayerListener {
    2. public static Main plugin;
    3.  
    4. String temp;
    5.  
    6. public MainPlayerListener(Main instance) {
    7. plugin = instance;
    8. }
    9.  
    10. public void onPlayerChat(PlayerChatEvent event) {
    11. String message = event.getMessage();
    12. String[] messageS = message.split(" ");
    13. String sender = event.getPlayer().getName();
    14. Player reciver = findPlayerByName(messageS[1]);
    15. if (reciver == null) {
    16. event.getPlayer().sendMessage(ChatColor.RED + "Player not online!");
    17. }
    18.  
    19. if (messageS[0].equalsIgnoreCase("/m")) {
    20. temp = arrayToString(messageS);
    21. reciver.sendMessage(ChatColor.GREEN + sender + ": " + temp);
    22. }
    23. }
    24.  
    25. private Player findPlayerByName(String playerName) {
    26. for (Player player : Bukkit.getServer().getOnlinePlayers()) {
    27. if (player.getName().equalsIgnoreCase(playerName)) {
    28. return player;
    29. }
    30. }
    31. return null;
    32. }
    33.  
    34. public String arrayToString(String[] array) {
    35.  
    36. StringBuilder sb = new StringBuilder();
    37. for (int i = 2; i < array.length; i++) {
    38. sb.append(array[i]);
    39. }
    40. String newString = sb.toString();
    41. return newString;
    42. }
    43. }[/i]
     
  12. Offline

    escape

    You didn't have the [ i] before though :p
     
  13. Offline

    stelar7

    @escape
    I know... STUPID forum changed it to an italic tag -.-"
     
  14. Offline

    escape

    Haha I know it did that to me too.
     
Thread Status:
Not open for further replies.

Share This Page