Best Ways To Convert Char To String In Java - MasterInJava

char to string in java

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

convert char to string in java

This method is one of the best and the recommended method by every Java developer.

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

how to convert char to string java

It is the oldest method to convert a char to string. In this method you have to write char and give it a name(I chose the same name "ko"). And assign its value within single inverted commas(in my case "s") and close it with a semi-colon.

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

how to convert char array to string java

This is a good method for converting a char to string but I don't recommend this method as it is quite lengthy.

 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

char array to string java

This is also one of the best and the recommended method to convert a char to string. In the last three method we have created char.

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

convert char array to string java

This method is pretty much similar to the previous method, but in this method we have to create char. So, in this method, we have to create a char and give it a name which is then followed by an equal sign and then typing its value(of your choice) within single inverted commas and ending the line with a semi-colon.

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.

Conclusion

There it was, the best way to convert char to string in java. I hope that you do like this post. Don't forget to comment and share this post with your friends and help them learn too. And if you have any doubt regarding this article you can contact me a or leave a comment.


Post a Comment

0 Comments