Implementation and use of js StringBuffer class, jsstringbuffer class

Source: Internet
Author: User

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

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.