Array join() method
Array toString method
Let us discuss both of these methods in detail with help of code examples −
Using the Array join() Method
The join() method joins all the elements of an array and forms a string and returns it later by separating every element with the specified separator passed to the method in form of a string.
Following syntax shows how we can use the join() method to get a comma−separated list of all array elements −
Let us discuss the parameters and the return value of the join() method −
separator − The join() method accepts a separator in the form of string. You can pass any separator of your choice to it like: dot, comma, and, or etc. If there is no any separator passed to this method, then by default it will separate all the elements with a comma(“,”) separator.
String − It will return a string containing all the array elements that are separated by the separator passed to the join() method.
Let us understand the practical implementation of join() method to turn the elements of a array in a comma-separated list −
Step 3 − In the third step, we will use the join() method inside the function declared in the previous step and display the result on user screen when user clicks the button.
Using the Array toString() Method
The toString() method simply converts all the elements of the array into a string and then return that string in the form of a comma-separated list which contains all the elements that contained by the original array. The toString() method does not accept any parameter as a separator to separate elements, it by default returns comma-separated list of all the elements of array.
The following syntax will be used to turn a array in comma-separated list using the toString() method −
Let us discuss the practical implementation of the toString() method to turn the array in a comma-separated list −
The algorithm of this example and the previous example is almost same, there is a slight change in the algorithm where you just need to replace the join() method in previous algorithm with the toString() method without passing any parameter to it.
As you can clearly see that, we used the same array and the function to implement this approach too. We have just replaced the join() method with toString() method. But there is no change in the result of both the approaches, In the end, we get a comma-separated list of all the elements of the array, which is the main motive of doing this all.
- Related Articles
- Convert String into comma separated List in Java
- How to create comma separated list from an array in PHP?
- How to convert array of comma separated strings to array of objects?
- Display MySQL Results as comma separated list?
- How to convert a comma separated String into an ArrayList in Java?
- How do I turn a list into an array in Java?
- How to set a comma separated list as a table in MySQL?
- Python program to convert list of string to comma separated string
- Java Program to Convert a List of String to Comma Separated String
- MySQL query to retrieve records from the part of a comma-separated list?
- How to create a comma separated string from a list of string in C#?