converting arrays and strings to each other

Source: Internet
Author: User

1, character array converted to string

Java can use two methods to convert a character array directly into a string.

Method 1: Convert directly when constructing a string.
char[] data = {' A ', ' B ', ' C '};
String str = new string (data);

Method 2: Call the method transformation of the string class.
string.valueof (char[] ch)

2, string array converted to string

If it is "string array" to "string", only through the loop, there is no other way

string[] str = {"abc", "BCD", "def"};

StringBuffer sb = new StringBuffer ();

for (int i = 0; i < str.length; i++) {

SB. Append (Str[i]);

}

String s = sb.tostring ();

3, string converted to character array

This depends on your specific needs. If there is a delimiter of the kind such as "a,b,c"; just split it straight.
String string = "A,b,c";
String [] stringarr= string.split (","); Note that separators are needed to translate the drops ...
If it is a string like "abc", it is directly
String string = "abc";
char [] Stringarr = String.tochararray (); Note The return value is a char array
If you want to return a byte array, use the GetBytes method directly OK ~ ~
String string = "abc";
byte [] Stringarr = String.getbytes ();

4, string converted to string array

/*** * @ClassName: arrayandstring * @Description: TODO (here is a word describing the role of this class) *@authorWilliam_dai * @date August 15, 2018 **/ Public classarrayandstring { Public Static voidMain (string[] args) {/**1, the array is converted to a string*/        //Method 1: Convert directly when constructing a string.         Char[] data = {' A ', ' B ', ' C '}; String Str=NewString (data);                System.out.println (str); //Method 2: Call the method transformation of the string class. String str2 =string.valueof (data);                System.out.println (STR2); /**2, string array converted to string*/        //if it is "string array" to "string", only through the loop, there is no other wayString[] Str3 = {"abc", "BCD", "Def"}; StringBuffer SB=NewStringBuffer ();  for(inti = 0; i < str3.length; i++) {sb. Append (Str3[i]); } String STR4=sb.tostring ();                System.out.println (STR4); /**3, string conversions to arrays*/        //This depends on your specific needs. If there is a delimiter of the kind such as "a,b,c"; just split it straight.String STR5 = "A,b,c"; String [] Strarr= Str5.split (",");  for(inti = 0; i < strarr.length; i + +) {System.out.print (Strarr[i]+" "); }        //if it is a string like "abc", it is directlyString STR6 = "abc" ; Char[] Stringarr = Str6.tochararray ();//Note The return value is a char array         for(inti = 0; i < stringarr.length; i + +) {System.out.print (Stringarr[i]+" "); }        //if you want to return a byte array, use the GetBytes method directly OK ~ ~String STR7 = "abc" ; byte[] Intarr =str7.getbytes ();  for(inti = 0; i < intarr.length; i + +) {System.out.print (Intarr[i]+" "); }        /**4, string converted to string array*/String str8= "Is You OK?"; String [] strArr2= Str8.split ("");  for(inti = 0; i < strarr2.length; i + +) {System.out.println (strarr2[i]); }    }}

converting arrays and strings to each other

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.