How to connect strings from an array in java

I’m creating a bukkit plugin and one of its features is to show the plugins on the server, here’s my code that handles the plugin listing:

for(int i = 0; i < plugins.length; i++){
        String conplugin = plugins[i].toString();
        String[] conplugin2 = conplugin.split(" ");
        if(i + 1 == plugins.length) {
        pluginlist.add(ChatColor.BLUE + conplugin2[0]);
        } else {
            pluginlist.add(ChatColor.BLUE + conplugin2[0] +  ChatColor.DARK_GRAY + ", " );
        }
    }

I want to get all the strings from the array (pluginlist) and make one string out of them.

Answer

If you want to construct a String from a String array, you could use a for loop and append the array element to the end of your new string.

StringBuilder newString = new StringBuilder ();

for (int i = 0; i < arr.length; i++) { 
    newString.append (arr [i]);
}
return newString;

You could also use a String, but depending on the size of the array of plugins, it would probably be faster to create a StringBuilder.

Leave a Reply

Your email address will not be published. Required fields are marked *