Home » how to convert char array to string in Java? | char array to string

how to convert char array to string in Java? | char array to string

In this post, I will learn how to convert char array to String in Java. A fresh String should be allocated that display the step by step sequence of characters details in the chars array. Since Simple Data String is immutable in Java Programming, the subsequent updation of the simply character main array does not affect the allocated data strings. we will learn about chars array to strings with an example.

There are four ways to convert a char array (char[]) to String using Java Examples:

char to string java, string to char array, array to string, char array to string, char to string
char array to string java

Using String Constructor

The simplest solution is to pass arguments the char array into the data Strings constructor. The data Strings Java constructor intramural uses Arrays.copyOf() function to copy the data contents of the character main array.

// Convert chars array to String in Java
class CharArrayToString
{
	public static void main(String[] args)
	{
		char[] mycharacters = {'P', 'a', 'k', 'a', 'i', 'n', ' ',
						'J', 'a', 'y', 'd', 'e', 'e', 'p'};

		String resultstr = new String(mycharacters);
		System.out.println(resultstr);
	}
}

String.valueOf() or String.copyValueOf()

I can encapsulate the String Java construtor call by using String.valueOf() functions or String.copyValueOf() which intramural does the alternative way.

// Convert chars array to String in Java
class CharArrayToString
{
	public static void main(String[] args)
	{
		char[] mycharacters = {'P', 'a', 'k', 'a', 'i', 'n', ' ',
						'J', 'a', 'y', 'd', 'e', 'e', 'p'};

		String resultstr = String.copyValueOf(mycharacters);
		System.out.println(resultstr);
	}
}

Using StringBuilder

And then great tiny way of converting a char array to Strings is using StringBuilder in Java. The idea is to iterate over the characters and append each char to a StringBuilder using Java. in last, call toString() method on the StringBuilder when iterated over all the chars.

Read Also:  Simple CSS Star Rating plugin

// Convert char array to String in Java
class CharArrayToString
{
	public static void main(String[] args)
	{
		char[] mycharacters = {'P', 'a', 'k', 'a', 'i', 'n', ' ',
						'J', 'a', 'y', 'd', 'e', 'e', 'p'};

		StringBuilder sb = new StringBuilder();
		for (char ch: mycharacters) {
			sb.append(ch);
		}

		String resultstr = sb.toString();
		System.out.println(resultstr);
	}
}

Using toString() + substring() + Regex

(100% I am Not recommended)

The main logic here is to read a data strings description of the specified array. The data strings description all the consists of a list of the array’s data elements, enclosed in square [] brackets like a technical ("[]") as well as all close to elements are separated by a comma (,) like by a space “, “.

Read Also:  How to Convert String to array in php Examples

I can simply fetch read of the all square brackets by calling simple substring() methods, as wel as like as a comma + space by using replaceAll() method using Java.

import java.util.Arrays;

// Convert char array to String in Java
class CharArrayToString
{
	public static void main(String[] args)
	{
		char[] mycharacters = {'P', 'a', 'k', 'a', 'i', 'n', ' ',
						'J', 'a', 'y', 'd', 'e', 'e', 'p'};

		String resultstr = Arrays.toString(mycharacters)
							.substring(1, 3*mycharacters.length-1)
							.replaceAll(", ", "");

		System.out.println(resultstr);
	}
}

Java: Two ways to Convert Char Array to String

Below There are 2 Basic ways you can simple source code with convert Char[] to Strings using Java Example.
Java Source Code:

package com.pakainfo;
 
/**
 * @author pakainfo.com
 */
 
public class PakainfoCharArrayToString {
 
	public static void main(String[] args) {
		char[] CharArrayToString = new char[] {'L', 'O', 'V', 'E', ' ',  'V', 'S', ' ', 'L', 'I', 'F', 'E'};
 
		String results = new String(CharArrayToString);
		System.out.println("results : " + results);
 
		String results2 = String.valueOf(CharArrayToString);
		System.out.println("\nresults2 : " + results2);
	}
}

Output:

results : LOVE VS LIFE
 
results2 : LOVE VS LIFE

I hope you get an idea about char array to string java.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  call javascript function from html

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Load web pages iframe using jQuery Example
  2. Read Also:  Bitcoin Altcoin Ethereum Cryptocurrency Exchange Script
  3. Read Also:  Vue js Laravel Image Upload Example Tutorial From Scratch
  4. Read Also:  Ajax Autocomplete Live Search Suggestion using PHP MySQL
  5. Read Also:  how to convert string to char c++ ?
  6. Read Also:  How to convert a String to Char Array in Java?
  7. Read Also:  Login and Registration Form in PHP using Mysqli
  8. Read Also:  Woocommerce Product publish, update and delete hooks
  9. Read Also:  PHP String Explode and Implode Function
  10. Read Also:  ErrorException in Builder.php[solution]

Leave a Comment

Your email address will not be published. Required fields are marked *