Implementation and use of js StringBuffer class, jsstringbuffer class
<Strong> There Is A StringBuffer class in JAVA, but js does not implement a simple js StringBuffer class. </Strong>
// Create a StringBuffer class. this class has two methods: append method and toString method function StringBuffer () {this. _ strings _ = [];}; StringBuffer. prototype. append = function (str) {this. _ strings __. push (str) ;}; StringBuffer. prototype. toString = function () {return this. _ strings __. join ('');};Use of StringBuffer:
// This instance is mainly used to splice strings
Function GnComCard (companymsg) {var buffer = new StringBuffer (); // declare an object buffer. append ('<li>'); // append a string, which is the same as buffer. append ('<div class = "com_photo"> <a href = "#" title = "user profile"> </img> </a> </div>'); buffer. append ('<div class = "com_msg">'); buffer. append ('<div> <a class = "com_comname">' + companymsg. companyMasterName + '</a> <span clas S = "com_time"> '+ FormatDate (companymsg. crtime) + '</span> </div>'); buffer. append ('</div> <div class = "news">'); if (companymsg. newsId! = Null) {buffer. append ('<a href = "' + companymsg. newsId + '"title =" news "class =" com_newsname ">' + companymsg. newsTitle + '</a>');} else {buffer. append ('<a href = "javascript: void (0)" class = "com_newsname">' + companymsg. newsTitle + '</a>');} if (companymsg. newsContent! = Null) {buffer. append ('<div class = "com_sh"> <span class = "toggle"> Expand </span> </div>'); buffer. append ('<div class = "com_newscontent">' + companymsg. newsContent + '</div>');} buffer. append ('</div>'); buffer. append ('<div class = "comment"> <a href = "javascript: void (0)" style = "float: left "title =" tracing company "> <span class =" track "> </span> </a> <input type =" text "> </input> </div> '); buffer. append ('</div> </li>'); return buffer. toString (); // concatenate a string}
It doesn't mean that the StringBuffer class can replace the String class. How can StringBuffer not use the toCharArray method?
API.
Public void getChars (int srcBegin,
Int srcEnd,
Char [] dst,
Int dstBegin) copies the characters from this sequence to the destination character array dst. The first character to be copied is at the index srcBegin; the last character to be copied is at the index srcEnd-1. The total number of characters to be copied is srcEnd-srcBegin. The character to be copied to the dst sub-array starts from the index dstBegin and ends with the following index:
Dstbegin + (srcEnd-srcBegin)-1
Parameters:
SrcBegin-copy from this offset.
SrcEnd-Stop copying at this offset.
Dst-an array used to save the copied data.
The offset in dstBegin-dst.
Throw:
NullPointerException-If dst is null.
IndexOutOfBoundsException-if any of the following values is true:
SrcBegin is negative.
DstBegin is negative.
The srcBegin parameter is greater than the srcEnd parameter.
SrcEnd is greater than this. length ().
DstBegin + srcEnd-srcBegin is greater than dst. length
-------------------------------------------------------------------------------
Define the array first:
StringBuffer sb = ......;
Char [] c = new char [sb. length ()];
Sb. getChars (0, c. length, c, 0 );
In JAVA, the StringBuffer class cannot input a group of numbers and then reverse the number.
Import java. io. IOException;
Import java. util. collections;
Public class B
{
Public static void main (String [] args) throws IOException
{
Required bytes = new bytes (System. in );
String str = response. next ();
StringBuilder builder = new StringBuilder ();
Char [] c = str. toCharArray ();
For (int I = c. length-1; I> = 0; I --){
Builder. append (c [I]);
}
System. out. println (builder. toString ());
}
}
Reclaim by yourself