ItzBenteThePig
ItzBenteThePig

Reputation: 746

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.

Upvotes: 0

Views: 228

Answers (2)

Theo
Theo

Reputation: 1193

Use a StringBuilder as it is mutable and you can append to it. Strings are immutable hence you can't change it.

You can use the previous StringBuilder example.

Upvotes: 0

user3969578
user3969578

Reputation:

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.

Upvotes: 1

Related Questions