Q:

How to convert a character array to string in Java?

belongs to collection: Java String Programs

0

Given a character array and we have to convert it to string.

In this example, we are taking a character array chrArr which is initializing with string character by character. And then, converting character array to string using new String(chrArr) and assigning to the string object str.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program:

import java.lang.*;

public class ConvertCharArrayToStringPrg
{
	public static void main(String []args)
	{
		// declare String object
		String str="";     
		// declare character array
		char [] chrArr= new char[]{'H','e','l','l','o'};   

		 // convert char array to string
		str= new String(chrArr);  

		//print string
		System.out.println("str : "+str);
	}
}

Output

str: Hello

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to calculate length of the string using String... >>
<< How to get length of the string in java?...