Javascript required
Skip to content Skip to sidebar Skip to footer

How to Turn a String Into an Array

Technical Questions and Answers

  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

How convert an array of Strings in a single String in java?


Using StringBuffer

  • Create an empty String Buffer object.

  • Traverse through the elements of the String array using loop.

  • In the loop, append each element of the array to the StringBuffer object using the append() method.

  • Finally convert the StringBuffer object to string using the toString() method.

Example

public class ArrayOfStrings {    public static void main(String args[]) {       String stringArray[] = {"Hello ", " how", " are", " you", " welcome", " to", " Tutorialspoint"};       StringBuffer sb = new StringBuffer();       for(int i = 0; i < stringArray.length; i++) {          sb.append(stringArray[i]);       }       String str = sb.toString();       System.out.println(str);    } }

Output

Hello how are you welcome to Tutorialspoint

Using the toString() method of the Arrays class

The toString() method of the Arrays class accepts a String array (in fact any array) and returns it as a String. Pass your String array to this method as a parameter.

Example

import java.util.Arrays; public class ArrayOfStrings {    public static void main(String args[]) {       String stringArray[] = {"Hello ", " how", " are", " you", " welcome", " to", " Tutorialspoint"};       StringBuffer sb = new StringBuffer();       for(int i = 0; i < stringArray.length; i++) {          sb.append(stringArray[i]);       }       String str = Arrays.toString(stringArray);       System.out.println(str);    } }

Output

Hello how are you welcome to Tutorialspoint

Using the StringJoiner class

Since Java8 StringJoiner class is introduced this you can construct a sequence of characters separated by desired delimiter.

The add() method accepts a CharacterSequence object (Segment, String, StringBuffer, StringBuilder) and adds it to the current Joiner separating the next and previous elements (if any) with delimiter at the time of constructing it.

The toString() method returns the contents of the current StringJoiner as a Sting object.

Therefore, to convert String array to a single Sting using this class −

  • Create an object of StringJoiner.

  • Traverse through the Sting array using a loop.

  • In the loop add each element of the Sting array to the StringJoiner object.

  • Convert the it to String using the toSting() method.

Example

import java.util.StringJoiner; public class ArrayOfStrings {    public static void main(String args[]) {       String stringArray[] = {"Hello", " how", " are", " you", " welcome", " to", " Tutorialspoint"};       StringJoiner joiner = new StringJoiner("");       for(int i = 0; i < stringArray.length; i++) {          joiner.add(stringArray[i]);       }       String str = joiner.toString();       System.out.println(str);    } }

Output

Hello how are you welcome to Tutorialspoint

raja

Published on 10-Oct-2019 07:59:20

  • Related Questions & Answers
  • Convert an ArrayList of String to a String array in Java
  • How to convert an array to string in java?
  • How to join two strings to convert to a single string in Python?
  • How to convert/store a string of numbers in to an array in java?
  • Convert Set of String to Array of String in Java
  • Write a program to convert an array to String in Java?
  • How to convert an array of characters into a string in C#?
  • C# program to convert several strings into a single comma-delimited string
  • How to convert a Double array to a String array in java?
  • How to convert comma seperated java string to an array.
  • C# Program to search for a string in an array of strings
  • How to convert string to array of integers in java?
  • How to convert array of decimal strings to array of integer strings without decimal in JavaScript
  • how to convert Object array to String array in java
  • How to convert a String to an int in Java

How to Turn a String Into an Array

Source: https://www.tutorialspoint.com/how-convert-an-array-of-strings-in-a-single-string-in-java