how to change array to string code example

Example 1: array to string java

char[] charArray = new char[] {'b','u','z','z'};

String answer = new String(charArray);

Example 2: java string array to one string

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);
   }
}

Example 3: java srting array to string

Arrays.toString(charArr);

Example 4: turn array into string

const elements = ['Fire', 'Air', 'Water'];

console.log(elements.join());
// expected output: "Fire,Air,Water"

console.log(elements.join(''));
// expected output: "FireAirWater"

console.log(elements.join('-'));
// expected output: "Fire-Air-Water"

Example 5: array to string

/**
 * Array.prototype.toString() method
 *
 * Returns a string representing the elements of the array. Arguments are not
 * expected and silently ignored. Utilized under the hood when an array are
 * referred like a string should.
 */
var array = [1, 2, 3, 'I', 'II', 'III', 'ūnus', 'duo', 'trēs']

/**
 * Expected output:
 * '1,2,3,I,II,III,ūnus,duo,trēs'
 * '1,2,3,I,II,III,ūnus,duo,trēs'
 * '1,2,3,I,II,III,ūnus,duo,trēs'
 */
console.log(
	'%s\n%s\n%s',
    array.toString(),
    String(array),
    array + ''
)

/**
 * Under the hood Array.prototype.toString() utilizes Array.prototype.join()
 * method if available and falls back to Object.prototype.toString() method
 * otherwise
 * 
 * Expected output:
 * none if Array.prototype.toString() utilizes Array.prototype.join() method,
 * 'Assertion failed: [1,2,3,"I","II","III","ūnus","duo","trēs"].toString()
 * utilizes fallback method Object.prototype.toString() instead of the default
 * .join()' otherwise
 */
console.assert(
    typeof array.toString === typeof array.join &&
    array.toString() === array.join(),
    '%o.%O utilizes fallback method %s.%O instead of the default .%O',
    array,
    array.toString,
    'Object.prototype',
    Object.prototype.toString,
    array.join || Array.prototype.join
)

/**
 * Expected output:
 * '1,2,3,I,II,III,ūnus,duo,trēs'
 * '[object Array]'
 */
console.log(
    '%s\n%s',
    array.toString(),
    (array.join = null) || array.toString()
)

/**
 * Kinda polyfill with barbaric static fallback if Array.prototype.join()
 * and Object.prototype.toString() methods are both unavailable
 */
Array.prototype.toString || 
    Object.defineProperty(Array.prototype, 'toString', {
        value : function toString () {
            return typeof this.join === 'function'
            	? this.join()
                : typeof Object.prototype.toString === 'function'
                	? Object.prototype.toString.call(this)
                    : '[object Array]'
        },
        configurable: true,
        writable: true
   })

Tags:

Java Example