cancel
Showing results for 
Search instead for 
Did you mean: 

convert StringBuffer to String[]

Former Member
0 Kudos

how to convert StringBuffer to String Array ?


StringBuffer strContent=new StringBuffer();
for(int i=0;i<3;i++){
strContent.append("This is new message:" +i);
}

//how to add to this
String[] strArrContent = {};

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Yzme,

we can differentiate between String and String buffer like , A string buffer is similar to String, but the contents can be modified at any point of time using some methods.

To convert a String buffer into a String we have toString() method.

for example :

x = "a" + 4 + "c"

is similar to

x = new StringBuffer().append("a").append(4).append("c")

.toString()

where x is a string..

reference :

<a href="http://java.sun.com/j2se/1.4.2/docs/api/">http://java.sun.com/j2se/1.4.2/docs/api/</a>

Do reward points..if this helps u in reaching at ur solution;

//

how to convert StringBuffer to String Array ?

StringBuffer strContent=new StringBuffer();

for(int i=0;i<3;i++){

strContent.append("This is new message:" +i);

}

//how to add to this

String[] strArrContent = {};

Former Member
0 Kudos

String[] strArrContent = new String[]{strContent.toString()};

prashil
Advisor
Advisor
0 Kudos

Hi Denis,

I think this will result, in a single element array.

If we output strArrContent.length it will give only 1. Is that correct?

What i understood by the requirement is that Yzme wants different messages

in different element of the array like

strArrContent[0] = "This is new message0";

strArrContent[1] = "This is new message1";

strArrContent[2] = "This is new message2";

Yzme, please verify your requirement.

Thanks,

Prashil

prashil
Advisor
Advisor
0 Kudos

Hi Yzme,

Put a seperater in between the strings like &,

<i>strConent.append("This is a new message"i"&");</i>

Now you can split the stringbuffer into an array;

<i>strArrContent[] = strContent.split("&");</i>

This will split the stringbuffer into elements of Array with a seperator as "&";

Regards,

Prashil