Conversion method for char array (character array) and string type in Java _java

Source: Internet
Author: User

The examples in this article describe the conversion methods for char arrays (character arrays) and string strings in Java. Share to everyone for your reference, specific as follows:

When you are programming in the Java language, when you use the Password field JPasswordField component, you need to use the GetPassword () method of that component if you want to obtain a password value. JPasswordField's GetPassword () method returns an array of char types, which we often need to convert to string type for operations such as password matching or password assignment. At this point, you need to convert an array of type char. Of course, you will often encounter situations where you convert a string type to a char array.

The following code, is relatively the most concise.

Cases:

String Strstringtype= "my string"; Create a String variable Strstringtype
char[] chrchararray;//Create a character array chrchararray
Chrchararray = Strstringtype.tochararray (); Converts a string variable to a character array
strstringtype= string.valueof (Chrchararray);//Convert a character array to a string

Here, the key is two methods.

(1) The ToCharArray () method of the String class, which converts a string to a character array
(2) The valueof () method of the String class converts an array of type char to a string

I hope this article will help you with Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.