Conversion of StringBuffer to string
1. Convert StringBuffer to String--stringbuffer class member The ToString function to convert it to a string type. Examples are as follows:
StringBuffer StringBuffer = new StringBuffer ("Hello World");
String C = stringbuffer.tostring (); Call the ToString function to convert a StringBuffer object to a String object
2. Convert String to StringBuffer
Method One: Using the constructor function
String str= "Hello world.";
StringBuffer buffer = new StringBuffer (str);
Mode two: Call the Append function
String str= "Hello world.";
StringBuffer buffer2 = new StringBuffer ();
Buffer2.append (str);
Conversion of string to character array
1. Convert a string into a character array--string class member ToCharArray function converts it to a character array. Examples are as follows:
String c = "Hello world"; /Create a String object
char[] cd = C.tochararray ();//re-call the ToCharArray function of the string object into a word
2. Converting a character array into String--java provides two ways to convert a character array directly to a string.
Method 1: Use the constructor of the string class to complete the transformation directly when the string is constructed.
char[] data = {' A ', ' B ', ' C '};
String str = new string (data);
Method 2: Call the ValueOf function transformation of the string class.
String str = string.valueof (char[] ch);
Converting StringBuffer to character arrays
1. Converting stringbuffer into character array--java does not support direct conversion from stringbuffer to character arrays. Instead, the StringBuffer is converted to a string, and then the ToCharArray function is converted to a character array by a string call. The conversion example is as follows:
StringBuffer StringBuffer = new StringBuffer ("Hello World");
String C = stringbuffer.tostring (); Convert a StringBuffer object to a String object first
char[] cd = C.tochararray (); Re-invoking the ToCharArray function of the string object into a character array
2. Converting a character array to stringbuffer--is similar to converting a stringbuffer into a character array, you need to convert the character array to a string and then convert the string to StringBuffer.
char[] data = {' H ', ' e ', ' l ', ' l ', ' o ', ' d '};
String str = new string ();
str.valueof (char[] ch); Call the valueof function of the string class to convert a character array into a string
StringBuffer buffer = new StringBuffer ();
Buffer.append (str); Call the Append function to convert a string into StringBuffer
Java String StringBuffer