Java - Append quotes to strings in an array and join strings in an array

Java - Append quotes to strings in an array and join strings in an array

To append quotes to strings in an array and then join them into a single string in Java, you can use a loop to process each element in the array and add quotes around it. After that, you can join the modified elements into a single string using a delimiter of your choice. Here's an example:

public class StringArrayManipulation { public static void main(String[] args) { String[] originalArray = {"apple", "banana", "cherry"}; // Append quotes to each element in the array for (int i = 0; i < originalArray.length; i++) { originalArray[i] = "\"" + originalArray[i] + "\""; } // Join the modified elements into a single string with a delimiter (e.g., comma) String joinedString = String.join(", ", originalArray); System.out.println(joinedString); } } 

In this example:

  1. We start with an array of strings named originalArray.

  2. We iterate through the elements of the array using a loop and append quotes (") to each element. The modified elements are stored back in the same array.

  3. Finally, we use String.join(delimiter, elements) to join the modified elements into a single string with a specified delimiter (in this case, a comma and a space).

When you run this code, it will produce the following output:

"apple", "banana", "cherry" 

The strings in the originalArray are enclosed in double quotes, and they are joined together into a single string separated by a comma and a space.

You can customize the delimiter or adjust the code to meet your specific requirements.


More Tags

tornado azure-pipelines-build-task voice schedule location argb updating nslayoutconstraint xslt-grouping pdf-reader

More Java Questions

More Organic chemistry Calculators

More Math Calculators

More Chemical thermodynamics Calculators

More Chemistry Calculators