Java String StringBuffer

Source: Internet
Author: User

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

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.