Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

In Java How to Convert Char Array to String (four ways) – char[] to String()

In Java How to Convert Char Array to String (four ways) - char[] to String()

Below are the four simple ways you can convert Char[] to String in Java.

  1. Using new String(char[]). Using String Constructor.
  2. Using valueOf(char[])
  3. Using StringBuilder()
  4. Create your own REGEX operation 🙂 Search and replace.

Please be careful about Arrays.toString(char[]), that returns a string representation of the contents of the specified array. The string representation consists of a list of the array’s elements, enclosed in square brackets (“[]”).

Java program:

Create file CrunchifyCharArrayToString.java. Put below code into it.

package crunchify.com.tutorials;

import java.util.Arrays;

/**
 * @author Crunchify.com
 * In Java How to Convert Char Array to String (four ways) - char[] to String()
 *
 */

public class CrunchifyCharArrayToString {

    public static void main(String[] args) {
        char[] crunchifyCharArray = new char[]{'T', 'H', 'I', 'S', ' ', 'I', 'S', ' ', 'C', 'R', 'U', 'N', 'C', 'H', 'I', 'F', 'Y'};

        // Method-1: Using new String(char[]). Using String Constructor.
                // Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.
        String crunchifyResult = new String(crunchifyCharArray);
        System.out.println("Result new String(char[]): " + crunchifyResult);

        // Method-2: Using valueOf(char[])
                // Returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the returned string.
        String crunchifyResult2 = String.valueOf(crunchifyCharArray);
        System.out.println("Result valueOf(char[]) : " + crunchifyResult2);

                // Method-3: Using StringBuilder()
                // Constructs a string builder with no characters in it and an initial capacity of 16 characters.
                StringBuilder crunchifyStringBuilder = new StringBuilder();
                for (char myCh: crunchifyCharArray) {
                        crunchifyStringBuilder.append(myCh);
                }
                String crunchifyResult3 = crunchifyStringBuilder.toString();
                System.out.println("Result StringBuilder() : " +crunchifyResult3);

                // Method-4: Create your own REGEX operation :) Search and replace
                String crunchifyResult4 = Arrays.toString(crunchifyCharArray)
                                .substring(1, 3*crunchifyCharArray.length-1)
                                .replaceAll(", ", "");

                System.out.println("Result custom method: regex replaceAll() : " + crunchifyResult4);

                // Be careful about Arrays.toString(char[])
                // Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]").
                String crunchifyResult5 = Arrays.toString(crunchifyCharArray);
                System.out.println("\nBe Careful about Arrays.toString(char[]) : " + crunchifyResult5);

        }
}

Eclipse Console Result:

Just run above program as a Java Application and you should see result like below.

Result new String(char[]): THIS IS CRUNCHIFY
Result valueOf(char[]) : THIS IS CRUNCHIFY
Result StringBuilder() : THIS IS CRUNCHIFY
Result custom method: regex replaceAll() : THIS IS CRUNCHIFY

Be Careful about Arrays.toString(char[]) : [T, H, I, S,  , I, S,  , C, R, U, N, C, H, I, F, Y]

Process finished with exit code 0

The post In Java How to Convert Char Array to String (four ways) – char[] to String() appeared first on Crunchify.

Enregistrer un commentaire

0 Commentaires