Want to know how to convert char array to string in java? Then you are on the right website. In this article, I will show you some of the best ways to convert char to string in Java. So, without any delay let's jump on to the first method:-
Using Character.toString() Method
Basically, in this method, you have to create a "char" and give it a name(for example 'ko') and assign its value which can be any letter within single inverted commas, I chose the letter "s" and end the line with a semi-colon.
On the second line create a string and give it a name(I named it "schar") and then put an equal sign "=" and type the method Character.toString(); and within the parentheses "()" type the name of the char(its 'ko' for me) which you have given in the previous line and end the line with semi-colon.
Write the output statement System.out.println(); and within the parentheses type the name of the string(its 'schar' for me) you have given.
Using String Concatenation
Create a string and give it a name(I named it same as before i.e. "schar")which followed by an equal sign and then put double inverted commas.
Next to the double inverted commas place a plus sign "+" and type the char name(its 'ko' for me) and end the line with a semi-colon.
Now write the output statement System.out.println(schar); as you have written for the previous method.
Using Wrapper Class
In this method, you have create a char and give it a name of your choice(in my case "ko") and then put an equal sign. After the equal sign, within single inverted commas assign its value ("s") and end it with a semi-colon.
On the second line, create a string and give it a name. Put an equal sign and type the word "new" and then again type "Character()" and within the parentheses type the char name(ko) and put a full-stop.
Then type toString() and end the line with a semi-colon. Finally, write the output statement System.out.println(schar); as we have done for the previous methods.
Using String.valueOf(char[]) Method
But in this method instead of creating a char we will create a string and give it a char value and then convert into string. Firstly we have to create a string and give it a name(for me it is "ko") and then put an equal sign.
And type the method String.valueOf(char[]); and within the parentheses, we need to add the word 'new' before char which is then followed by square brackets "[]".
Next to the square brackets put a brace "{}" and within the brace type the value of the char within single inverted commas(for me it is "s") and end it with and semi-colon. Now, finally, type the output statement System.out.println(); where within the parentheses type the string name.
Using String.valueOf() Method
In the next line, we need to create a string and name it and then put a equal sign and type the method String.valueOf() where within the parentheses type the name of the char and end it with a semi-colon.
Finally, type the output statement System.out.println(); where within the parentheses type the name of the string.
0 Comments
masterinjavaofficial@gmail.com